11import { hasTracingEnabled } from '@sentry/core' ;
22import type { BrowserOptions } from '@sentry/svelte' ;
3- import { BrowserTracing , WINDOW , getCurrentScope , init as initSvelteSdk } from '@sentry/svelte' ;
4- import { addOrUpdateIntegration } from '@sentry/utils' ;
3+ import { getDefaultIntegrations as getDefaultSvelteIntegrations } from '@sentry/svelte' ;
4+ import { WINDOW , getCurrentScope , init as initSvelteSdk } from '@sentry/svelte' ;
5+ import type { Integration } from '@sentry/types' ;
56
67import { applySdkMetadata } from '../common/metadata' ;
7- import { svelteKitRoutingInstrumentation } from './router ' ;
8+ import { BrowserTracing } from './browserTracingIntegration ' ;
89
910type WindowWithSentryFetchProxy = typeof WINDOW & {
1011 _sentryFetchProxy ?: typeof fetch ;
@@ -19,15 +20,20 @@ declare const __SENTRY_TRACING__: boolean;
1920 * @param options Configuration options for the SDK.
2021 */
2122export function init ( options : BrowserOptions ) : void {
22- applySdkMetadata ( options , [ 'sveltekit' , 'svelte' ] ) ;
23+ const opts = {
24+ defaultIntegrations : getDefaultIntegrations ( options ) ,
25+ ...options ,
26+ } ;
2327
24- addClientIntegrations ( options ) ;
28+ applySdkMetadata ( opts , [ 'sveltekit' , 'svelte' ] ) ;
29+
30+ fixBrowserTracingIntegration ( opts ) ;
2531
2632 // 1. Switch window.fetch to our fetch proxy we injected earlier
2733 const actualFetch = switchToFetchProxy ( ) ;
2834
2935 // 2. Initialize the SDK which will instrument our proxy
30- initSvelteSdk ( options ) ;
36+ initSvelteSdk ( opts ) ;
3137
3238 // 3. Restore the original fetch now that our proxy is instrumented
3339 if ( actualFetch ) {
@@ -37,24 +43,49 @@ export function init(options: BrowserOptions): void {
3743 getCurrentScope ( ) . setTag ( 'runtime' , 'browser' ) ;
3844}
3945
40- function addClientIntegrations ( options : BrowserOptions ) : void {
41- let integrations = options . integrations || [ ] ;
46+ // TODO v8: Remove this again
47+ // We need to handle BrowserTracing passed to `integrations` that comes from `@sentry/tracing`, not `@sentry/sveltekit` :(
48+ function fixBrowserTracingIntegration ( options : BrowserOptions ) : void {
49+ const { integrations } = options ;
50+ if ( ! integrations ) {
51+ return ;
52+ }
53+
54+ if ( Array . isArray ( integrations ) ) {
55+ options . integrations = maybeUpdateBrowserTracingIntegration ( integrations ) ;
56+ } else {
57+ options . integrations = defaultIntegrations => {
58+ const userFinalIntegrations = integrations ( defaultIntegrations ) ;
59+
60+ return maybeUpdateBrowserTracingIntegration ( userFinalIntegrations ) ;
61+ } ;
62+ }
63+ }
64+
65+ function maybeUpdateBrowserTracingIntegration ( integrations : Integration [ ] ) : Integration [ ] {
66+ const browserTracing = integrations . find ( integration => integration . name === 'BrowserTracing' ) ;
67+ // If BrowserTracing was added, but it is not our forked version,
68+ // replace it with our forked version with the same options
69+ if ( browserTracing && ! ( browserTracing instanceof BrowserTracing ) ) {
70+ const options : ConstructorParameters < typeof BrowserTracing > [ 0 ] = ( browserTracing as BrowserTracing ) . options ;
71+ // This option is overwritten by the custom integration
72+ delete options . routingInstrumentation ;
73+ integrations [ integrations . indexOf ( browserTracing ) ] = new BrowserTracing ( options ) ;
74+ }
75+
76+ return integrations ;
77+ }
4278
43- // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false",
44- // in which case everything inside will get treeshaken away
79+ function getDefaultIntegrations ( options : BrowserOptions ) : Integration [ ] | undefined {
80+ // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
81+ // will get treeshaken away
4582 if ( typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__ ) {
4683 if ( hasTracingEnabled ( options ) ) {
47- const defaultBrowserTracingIntegration = new BrowserTracing ( {
48- routingInstrumentation : svelteKitRoutingInstrumentation ,
49- } ) ;
50-
51- integrations = addOrUpdateIntegration ( defaultBrowserTracingIntegration , integrations , {
52- 'options.routingInstrumentation' : svelteKitRoutingInstrumentation ,
53- } ) ;
84+ return [ ...getDefaultSvelteIntegrations ( options ) , new BrowserTracing ( ) ] ;
5485 }
5586 }
5687
57- options . integrations = integrations ;
88+ return undefined ;
5889}
5990
6091/**
0 commit comments