wrapGetInitialPropsWithSentry.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _nullishCoalesce, _asyncNullishCoalesce, _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
  8. *
  9. * @param origGetInitialProps The user's `getInitialProps` function
  10. * @param parameterizedRoute The page's parameterized route
  11. * @returns A wrapped version of the function
  12. */
  13. function wrapGetInitialPropsWithSentry(origGetInitialProps) {
  14. return new Proxy(origGetInitialProps, {
  15. apply: async (wrappingTarget, thisArg, args) => {
  16. if (isBuild()) {
  17. return wrappingTarget.apply(thisArg, args);
  18. }
  19. addTracingExtensions();
  20. const [context] = args;
  21. const { req, res } = context;
  22. const errorWrappedGetInitialProps = withErrorInstrumentation(wrappingTarget);
  23. const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
  24. // Generally we can assume that `req` and `res` are always defined on the server:
  25. // https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
  26. // This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
  27. // span with each other when there are no req or res objects, we simply do not trace them at all here.
  28. if (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
  29. const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
  30. dataFetcherRouteName: context.pathname,
  31. requestedRouteName: context.pathname,
  32. dataFetchingMethodName: 'getInitialProps',
  33. });
  34. const initialProps
  35. = await _asyncNullishCoalesce((await tracedGetInitialProps.apply(thisArg, args)), async () => ( {})); // Next.js allows undefined to be returned from a getInitialPropsFunction.
  36. const activeSpan = getActiveSpan();
  37. const requestSpan = _nullishCoalesce(getSpanFromRequest(req), () => ( (activeSpan ? getRootSpan(activeSpan) : undefined)));
  38. if (requestSpan) {
  39. initialProps._sentryTraceData = spanToTraceHeader(requestSpan);
  40. const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestSpan);
  41. initialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
  42. }
  43. return initialProps;
  44. } else {
  45. return errorWrappedGetInitialProps.apply(thisArg, args);
  46. }
  47. },
  48. });
  49. }
  50. /**
  51. * @deprecated Use `wrapGetInitialPropsWithSentry` instead.
  52. */
  53. const withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry;
  54. export { withSentryServerSideGetInitialProps, wrapGetInitialPropsWithSentry };
  55. //# sourceMappingURL=wrapGetInitialPropsWithSentry.js.map