wrapAppGetInitialPropsWithSentry.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
  2. import { addTracingExtensions, getActiveSpan, spanToTraceHeader, getDynamicSamplingContextFromSpan, getClient, getRootSpan } from '@sentry/core';
  3. import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
  4. import { isBuild } from './utils/isBuild.js';
  5. import { withTracedServerSideDataFetcher, getSpanFromRequest, withErrorInstrumentation } from './utils/wrapperUtils.js';
  6. /**
  7. * Create a wrapped version of the user's exported `getInitialProps` function in
  8. * a custom app ("_app.js").
  9. *
  10. * @param origAppGetInitialProps The user's `getInitialProps` function
  11. * @param parameterizedRoute The page's parameterized route
  12. * @returns A wrapped version of the function
  13. */
  14. function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps) {
  15. return new Proxy(origAppGetInitialProps, {
  16. apply: async (wrappingTarget, thisArg, args) => {
  17. if (isBuild()) {
  18. return wrappingTarget.apply(thisArg, args);
  19. }
  20. addTracingExtensions();
  21. const [context] = args;
  22. const { req, res } = context.ctx;
  23. const errorWrappedAppGetInitialProps = withErrorInstrumentation(wrappingTarget);
  24. const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
  25. // Generally we can assume that `req` and `res` are always defined on the server:
  26. // https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
  27. // This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
  28. // span with each other when there are no req or res objects, we simply do not trace them at all here.
  29. if (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
  30. const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedAppGetInitialProps, req, res, {
  31. dataFetcherRouteName: '/_app',
  32. requestedRouteName: context.ctx.pathname,
  33. dataFetchingMethodName: 'getInitialProps',
  34. });
  35. const appGetInitialProps
  36. = await tracedGetInitialProps.apply(thisArg, args);
  37. const activeSpan = getActiveSpan();
  38. const requestSpan = _nullishCoalesce(getSpanFromRequest(req), () => ( (activeSpan ? getRootSpan(activeSpan) : undefined)));
  39. // Per definition, `pageProps` is not optional, however an increased amount of users doesn't seem to call
  40. // `App.getInitialProps(appContext)` in their custom `_app` pages which is required as per
  41. // https://nextjs.org/docs/advanced-features/custom-app - resulting in missing `pageProps`.
  42. // For this reason, we just handle the case where `pageProps` doesn't exist explicitly.
  43. if (!appGetInitialProps.pageProps) {
  44. appGetInitialProps.pageProps = {};
  45. }
  46. if (requestSpan) {
  47. appGetInitialProps.pageProps._sentryTraceData = spanToTraceHeader(requestSpan);
  48. const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
  49. appGetInitialProps.pageProps._sentryBaggage =
  50. dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
  51. }
  52. return appGetInitialProps;
  53. } else {
  54. return errorWrappedAppGetInitialProps.apply(thisArg, args);
  55. }
  56. },
  57. });
  58. }
  59. /**
  60. * @deprecated Use `wrapAppGetInitialPropsWithSentry` instead.
  61. */
  62. const withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry;
  63. export { withSentryServerSideAppGetInitialProps, wrapAppGetInitialPropsWithSentry };
  64. //# sourceMappingURL=wrapAppGetInitialPropsWithSentry.js.map