wrapGetInitialPropsWithSentry.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var {
  2. _nullishCoalesce,
  3. _asyncNullishCoalesce,
  4. _optionalChain
  5. } = require('@sentry/utils');
  6. Object.defineProperty(exports, '__esModule', { value: true });
  7. const core = require('@sentry/core');
  8. const utils = require('@sentry/utils');
  9. const isBuild = require('./utils/isBuild.js');
  10. const wrapperUtils = require('./utils/wrapperUtils.js');
  11. /**
  12. * Create a wrapped version of the user's exported `getInitialProps` function
  13. *
  14. * @param origGetInitialProps The user's `getInitialProps` function
  15. * @param parameterizedRoute The page's parameterized route
  16. * @returns A wrapped version of the function
  17. */
  18. function wrapGetInitialPropsWithSentry(origGetInitialProps) {
  19. return new Proxy(origGetInitialProps, {
  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;
  27. const errorWrappedGetInitialProps = 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(errorWrappedGetInitialProps, req, res, {
  35. dataFetcherRouteName: context.pathname,
  36. requestedRouteName: context.pathname,
  37. dataFetchingMethodName: 'getInitialProps',
  38. });
  39. const initialProps
  40. = await _asyncNullishCoalesce((await tracedGetInitialProps.apply(thisArg, args)), async () => ( {})); // Next.js allows undefined to be returned from a getInitialPropsFunction.
  41. const activeSpan = core.getActiveSpan();
  42. const requestSpan = _nullishCoalesce(wrapperUtils.getSpanFromRequest(req), () => ( (activeSpan ? core.getRootSpan(activeSpan) : undefined)));
  43. if (requestSpan) {
  44. initialProps._sentryTraceData = core.spanToTraceHeader(requestSpan);
  45. const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(requestSpan);
  46. initialProps._sentryBaggage = utils.dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
  47. }
  48. return initialProps;
  49. } else {
  50. return errorWrappedGetInitialProps.apply(thisArg, args);
  51. }
  52. },
  53. });
  54. }
  55. /**
  56. * @deprecated Use `wrapGetInitialPropsWithSentry` instead.
  57. */
  58. const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry;
  59. exports.withSentryServerSideGetInitialProps = withSentryServerSideGetInitialProps;
  60. exports.wrapGetInitialPropsWithSentry = wrapGetInitialPropsWithSentry;
  61. //# sourceMappingURL=wrapGetInitialPropsWithSentry.js.map