wrapDocumentGetInitialPropsWithSentry.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const core = require('@sentry/core');
  6. const isBuild = require('./utils/isBuild.js');
  7. const wrapperUtils = require('./utils/wrapperUtils.js');
  8. /**
  9. * Create a wrapped version of the user's exported `getInitialProps` function in
  10. * a custom document ("_document.js").
  11. *
  12. * @param origDocumentGetInitialProps The user's `getInitialProps` function
  13. * @param parameterizedRoute The page's parameterized route
  14. * @returns A wrapped version of the function
  15. */
  16. function wrapDocumentGetInitialPropsWithSentry(
  17. origDocumentGetInitialProps,
  18. ) {
  19. return new Proxy(origDocumentGetInitialProps, {
  20. apply: (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: '/_document',
  36. requestedRouteName: context.pathname,
  37. dataFetchingMethodName: 'getInitialProps',
  38. });
  39. return tracedGetInitialProps.apply(thisArg, args);
  40. } else {
  41. return errorWrappedGetInitialProps.apply(thisArg, args);
  42. }
  43. },
  44. });
  45. }
  46. /**
  47. * @deprecated Use `wrapDocumentGetInitialPropsWithSentry` instead.
  48. */
  49. const withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry;
  50. exports.withSentryServerSideDocumentGetInitialProps = withSentryServerSideDocumentGetInitialProps;
  51. exports.wrapDocumentGetInitialPropsWithSentry = wrapDocumentGetInitialPropsWithSentry;
  52. //# sourceMappingURL=wrapDocumentGetInitialPropsWithSentry.js.map