wrapDocumentGetInitialPropsWithSentry.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { _optionalChain } from '@sentry/utils';
  2. import { addTracingExtensions, getClient } from '@sentry/core';
  3. import { isBuild } from './utils/isBuild.js';
  4. import { withTracedServerSideDataFetcher, withErrorInstrumentation } from './utils/wrapperUtils.js';
  5. /**
  6. * Create a wrapped version of the user's exported `getInitialProps` function in
  7. * a custom document ("_document.js").
  8. *
  9. * @param origDocumentGetInitialProps The user's `getInitialProps` function
  10. * @param parameterizedRoute The page's parameterized route
  11. * @returns A wrapped version of the function
  12. */
  13. function wrapDocumentGetInitialPropsWithSentry(
  14. origDocumentGetInitialProps,
  15. ) {
  16. return new Proxy(origDocumentGetInitialProps, {
  17. apply: (wrappingTarget, thisArg, args) => {
  18. if (isBuild()) {
  19. return wrappingTarget.apply(thisArg, args);
  20. }
  21. addTracingExtensions();
  22. const [context] = args;
  23. const { req, res } = context;
  24. const errorWrappedGetInitialProps = withErrorInstrumentation(wrappingTarget);
  25. const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
  26. // Generally we can assume that `req` and `res` are always defined on the server:
  27. // https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
  28. // This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
  29. // span with each other when there are no req or res objects, we simply do not trace them at all here.
  30. if (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
  31. const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
  32. dataFetcherRouteName: '/_document',
  33. requestedRouteName: context.pathname,
  34. dataFetchingMethodName: 'getInitialProps',
  35. });
  36. return tracedGetInitialProps.apply(thisArg, args);
  37. } else {
  38. return errorWrappedGetInitialProps.apply(thisArg, args);
  39. }
  40. },
  41. });
  42. }
  43. /**
  44. * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
  45. */
  46. const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry;
  47. export { withSentryServerSideDocumentGetInitialProps, wrapDocumentGetInitialPropsWithSentry };
  48. //# sourceMappingURL=wrapDocumentGetInitialPropsWithSentry.js.map