wrapAppGetInitialPropsWithSentry.js 3.6 KB

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