index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const react = require('@sentry/react');
  4. const devErrorSymbolicationEventProcessor = require('../common/devErrorSymbolicationEventProcessor.js');
  5. const getVercelEnv = require('../common/getVercelEnv.js');
  6. const browserTracingIntegration = require('./browserTracingIntegration.js');
  7. const rewriteFramesIntegration = require('./rewriteFramesIntegration.js');
  8. const tunnelRoute = require('./tunnelRoute.js');
  9. const nextRoutingInstrumentation = require('./routing/nextRoutingInstrumentation.js');
  10. const _error = require('../common/_error.js');
  11. const wrapGetStaticPropsWithSentry = require('../common/wrapGetStaticPropsWithSentry.js');
  12. const wrapGetInitialPropsWithSentry = require('../common/wrapGetInitialPropsWithSentry.js');
  13. const wrapAppGetInitialPropsWithSentry = require('../common/wrapAppGetInitialPropsWithSentry.js');
  14. const wrapDocumentGetInitialPropsWithSentry = require('../common/wrapDocumentGetInitialPropsWithSentry.js');
  15. const wrapErrorGetInitialPropsWithSentry = require('../common/wrapErrorGetInitialPropsWithSentry.js');
  16. const wrapGetServerSidePropsWithSentry = require('../common/wrapGetServerSidePropsWithSentry.js');
  17. const wrapServerComponentWithSentry = require('../common/wrapServerComponentWithSentry.js');
  18. const wrapRouteHandlerWithSentry = require('../common/wrapRouteHandlerWithSentry.js');
  19. const wrapApiHandlerWithSentryVercelCrons = require('../common/wrapApiHandlerWithSentryVercelCrons.js');
  20. const wrapMiddlewareWithSentry = require('../common/wrapMiddlewareWithSentry.js');
  21. const wrapPageComponentWithSentry = require('../common/wrapPageComponentWithSentry.js');
  22. const wrapGenerationFunctionWithSentry = require('../common/wrapGenerationFunctionWithSentry.js');
  23. const withServerActionInstrumentation = require('../common/withServerActionInstrumentation.js');
  24. /** @deprecated Import the integration function directly, e.g. `inboundFiltersIntegration()` instead of `new Integrations.InboundFilter(). */
  25. const Integrations = {
  26. // eslint-disable-next-line deprecation/deprecation
  27. ...react.Integrations,
  28. BrowserTracing: browserTracingIntegration.BrowserTracing,
  29. };
  30. // Treeshakable guard to remove all code related to tracing
  31. /** Inits the Sentry NextJS SDK on the browser with the React SDK. */
  32. function init(options) {
  33. const opts = {
  34. environment: getVercelEnv.getVercelEnv(true) || process.env.NODE_ENV,
  35. defaultIntegrations: getDefaultIntegrations(options),
  36. ...options,
  37. };
  38. fixBrowserTracingIntegration(opts);
  39. tunnelRoute.applyTunnelRouteOption(opts);
  40. core.applySdkMetadata(opts, 'nextjs', ['nextjs', 'react']);
  41. react.init(opts);
  42. const scope = react.getCurrentScope();
  43. scope.setTag('runtime', 'browser');
  44. const filterTransactions = event =>
  45. event.type === 'transaction' && event.transaction === '/404' ? null : event;
  46. filterTransactions.id = 'NextClient404Filter';
  47. scope.addEventProcessor(filterTransactions);
  48. if (process.env.NODE_ENV === 'development') {
  49. scope.addEventProcessor(devErrorSymbolicationEventProcessor.devErrorSymbolicationEventProcessor);
  50. }
  51. }
  52. // TODO v8: Remove this again
  53. // We need to handle BrowserTracing passed to `integrations` that comes from `@sentry/tracing`, not `@sentry/nextjs` :(
  54. function fixBrowserTracingIntegration(options) {
  55. const { integrations } = options;
  56. if (!integrations) {
  57. return;
  58. }
  59. if (Array.isArray(integrations)) {
  60. options.integrations = maybeUpdateBrowserTracingIntegration(integrations);
  61. } else {
  62. options.integrations = defaultIntegrations => {
  63. const userFinalIntegrations = integrations(defaultIntegrations);
  64. return maybeUpdateBrowserTracingIntegration(userFinalIntegrations);
  65. };
  66. }
  67. }
  68. function isNewBrowserTracingIntegration(
  69. integration,
  70. ) {
  71. // eslint-disable-next-line deprecation/deprecation
  72. return !!integration.afterAllSetup && !!(integration ).options;
  73. }
  74. function maybeUpdateBrowserTracingIntegration(integrations) {
  75. const browserTracing = integrations.find(integration => integration.name === 'BrowserTracing');
  76. if (!browserTracing) {
  77. return integrations;
  78. }
  79. // If `browserTracingIntegration()` was added, we need to force-convert it to our custom one
  80. if (isNewBrowserTracingIntegration(browserTracing)) {
  81. const { options } = browserTracing;
  82. // eslint-disable-next-line deprecation/deprecation
  83. integrations[integrations.indexOf(browserTracing)] = new browserTracingIntegration.BrowserTracing(options);
  84. }
  85. // If BrowserTracing was added, but it is not our forked version,
  86. // replace it with our forked version with the same options
  87. // eslint-disable-next-line deprecation/deprecation
  88. if (!(browserTracing instanceof browserTracingIntegration.BrowserTracing)) {
  89. // eslint-disable-next-line deprecation/deprecation
  90. const options = (browserTracing ).options;
  91. // This option is overwritten by the custom integration
  92. delete options.routingInstrumentation;
  93. // eslint-disable-next-line deprecation/deprecation
  94. delete options.tracingOrigins;
  95. // eslint-disable-next-line deprecation/deprecation
  96. integrations[integrations.indexOf(browserTracing)] = new browserTracingIntegration.BrowserTracing(options);
  97. }
  98. return integrations;
  99. }
  100. function getDefaultIntegrations(options) {
  101. const customDefaultIntegrations = [...react.getDefaultIntegrations(options), rewriteFramesIntegration.rewriteFramesIntegration()];
  102. // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
  103. // will get treeshaken away
  104. if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
  105. if (core.hasTracingEnabled(options)) {
  106. customDefaultIntegrations.push(browserTracingIntegration.browserTracingIntegration());
  107. }
  108. }
  109. return customDefaultIntegrations;
  110. }
  111. /**
  112. * Just a passthrough in case this is imported from the client.
  113. */
  114. function withSentryConfig(exportedUserNextConfig) {
  115. return exportedUserNextConfig;
  116. }
  117. exports.BrowserTracing = browserTracingIntegration.BrowserTracing;
  118. exports.browserTracingIntegration = browserTracingIntegration.browserTracingIntegration;
  119. exports.rewriteFramesIntegration = rewriteFramesIntegration.rewriteFramesIntegration;
  120. exports.nextRouterInstrumentation = nextRoutingInstrumentation.nextRouterInstrumentation;
  121. exports.captureUnderscoreErrorException = _error.captureUnderscoreErrorException;
  122. exports.withSentryGetStaticProps = wrapGetStaticPropsWithSentry.withSentryGetStaticProps;
  123. exports.wrapGetStaticPropsWithSentry = wrapGetStaticPropsWithSentry.wrapGetStaticPropsWithSentry;
  124. exports.withSentryServerSideGetInitialProps = wrapGetInitialPropsWithSentry.withSentryServerSideGetInitialProps;
  125. exports.wrapGetInitialPropsWithSentry = wrapGetInitialPropsWithSentry.wrapGetInitialPropsWithSentry;
  126. exports.withSentryServerSideAppGetInitialProps = wrapAppGetInitialPropsWithSentry.withSentryServerSideAppGetInitialProps;
  127. exports.wrapAppGetInitialPropsWithSentry = wrapAppGetInitialPropsWithSentry.wrapAppGetInitialPropsWithSentry;
  128. exports.withSentryServerSideDocumentGetInitialProps = wrapDocumentGetInitialPropsWithSentry.withSentryServerSideDocumentGetInitialProps;
  129. exports.wrapDocumentGetInitialPropsWithSentry = wrapDocumentGetInitialPropsWithSentry.wrapDocumentGetInitialPropsWithSentry;
  130. exports.withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry.withSentryServerSideErrorGetInitialProps;
  131. exports.wrapErrorGetInitialPropsWithSentry = wrapErrorGetInitialPropsWithSentry.wrapErrorGetInitialPropsWithSentry;
  132. exports.withSentryGetServerSideProps = wrapGetServerSidePropsWithSentry.withSentryGetServerSideProps;
  133. exports.wrapGetServerSidePropsWithSentry = wrapGetServerSidePropsWithSentry.wrapGetServerSidePropsWithSentry;
  134. exports.wrapServerComponentWithSentry = wrapServerComponentWithSentry.wrapServerComponentWithSentry;
  135. exports.wrapRouteHandlerWithSentry = wrapRouteHandlerWithSentry.wrapRouteHandlerWithSentry;
  136. exports.wrapApiHandlerWithSentryVercelCrons = wrapApiHandlerWithSentryVercelCrons.wrapApiHandlerWithSentryVercelCrons;
  137. exports.wrapMiddlewareWithSentry = wrapMiddlewareWithSentry.wrapMiddlewareWithSentry;
  138. exports.wrapPageComponentWithSentry = wrapPageComponentWithSentry.wrapPageComponentWithSentry;
  139. exports.wrapGenerationFunctionWithSentry = wrapGenerationFunctionWithSentry.wrapGenerationFunctionWithSentry;
  140. exports.withServerActionInstrumentation = withServerActionInstrumentation.withServerActionInstrumentation;
  141. exports.Integrations = Integrations;
  142. exports.init = init;
  143. exports.withSentryConfig = withSentryConfig;
  144. for (const k in react) {
  145. if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = react[k];
  146. }
  147. //# sourceMappingURL=index.js.map