browserTracingIntegration.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { _optionalChain } from '@sentry/utils';
  2. import { BrowserTracing as BrowserTracing$1, defaultRequestInstrumentationOptions, browserTracingIntegration as browserTracingIntegration$1, startBrowserTracingPageLoadSpan, startBrowserTracingNavigationSpan } from '@sentry/react';
  3. import { nextRouterInstrumentation } from './routing/nextRoutingInstrumentation.js';
  4. /**
  5. * A custom BrowserTracing integration for Next.js.
  6. *
  7. * @deprecated Use `browserTracingIntegration` instead.
  8. */
  9. // eslint-disable-next-line deprecation/deprecation
  10. class BrowserTracing extends BrowserTracing$1 {
  11. // eslint-disable-next-line deprecation/deprecation
  12. constructor(options) {
  13. super({
  14. // eslint-disable-next-line deprecation/deprecation
  15. tracingOrigins:
  16. process.env.NODE_ENV === 'development'
  17. ? [
  18. // Will match any URL that contains "localhost" but not "webpack.hot-update.json" - The webpack dev-server
  19. // has cors and it doesn't like extra headers when it's accessed from a different URL.
  20. // TODO(v8): Ideally we rework our tracePropagationTargets logic so this hack won't be necessary anymore (see issue #9764)
  21. /^(?=.*localhost)(?!.*webpack\.hot-update\.json).*/,
  22. /^\/(?!\/)/,
  23. ]
  24. : // eslint-disable-next-line deprecation/deprecation
  25. [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
  26. // eslint-disable-next-line deprecation/deprecation
  27. routingInstrumentation: nextRouterInstrumentation,
  28. ...options,
  29. });
  30. }
  31. }
  32. /**
  33. * A custom BrowserTracing integration for Next.js.
  34. */
  35. function browserTracingIntegration(
  36. options,
  37. ) {
  38. const browserTracingIntegrationInstance = browserTracingIntegration$1({
  39. // eslint-disable-next-line deprecation/deprecation
  40. tracingOrigins:
  41. process.env.NODE_ENV === 'development'
  42. ? [
  43. // Will match any URL that contains "localhost" but not "webpack.hot-update.json" - The webpack dev-server
  44. // has cors and it doesn't like extra headers when it's accessed from a different URL.
  45. // TODO(v8): Ideally we rework our tracePropagationTargets logic so this hack won't be necessary anymore (see issue #9764)
  46. /^(?=.*localhost)(?!.*webpack\.hot-update\.json).*/,
  47. /^\/(?!\/)/,
  48. ]
  49. : // eslint-disable-next-line deprecation/deprecation
  50. [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
  51. ...options,
  52. instrumentNavigation: false,
  53. instrumentPageLoad: false,
  54. });
  55. return {
  56. ...browserTracingIntegrationInstance,
  57. afterAllSetup(client) {
  58. const startPageloadCallback = (startSpanOptions) => {
  59. startBrowserTracingPageLoadSpan(client, startSpanOptions);
  60. };
  61. const startNavigationCallback = (startSpanOptions) => {
  62. startBrowserTracingNavigationSpan(client, startSpanOptions);
  63. };
  64. // We need to run the navigation span instrumentation before the `afterAllSetup` hook on the normal browser
  65. // tracing integration because we need to ensure the order of execution is as follows:
  66. // Instrumentation to start span on RSC fetch request runs -> Instrumentation to put tracing headers from active span on fetch runs
  67. // If it were the other way around, the RSC fetch request would not receive the tracing headers from the navigation transaction.
  68. // eslint-disable-next-line deprecation/deprecation
  69. nextRouterInstrumentation(
  70. () => undefined,
  71. false,
  72. _optionalChain([options, 'optionalAccess', _ => _.instrumentNavigation]),
  73. startPageloadCallback,
  74. startNavigationCallback,
  75. );
  76. browserTracingIntegrationInstance.afterAllSetup(client);
  77. // eslint-disable-next-line deprecation/deprecation
  78. nextRouterInstrumentation(
  79. () => undefined,
  80. _optionalChain([options, 'optionalAccess', _2 => _2.instrumentPageLoad]),
  81. false,
  82. startPageloadCallback,
  83. startNavigationCallback,
  84. );
  85. },
  86. };
  87. }
  88. export { BrowserTracing, browserTracingIntegration };
  89. //# sourceMappingURL=browserTracingIntegration.js.map