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