@@ -3,7 +3,6 @@ import type { Client, IntegrationFn, Span, StartSpanOptions, TransactionSource,
3
3
import {
4
4
addNonEnumerableProperty ,
5
5
browserPerformanceTimeOrigin ,
6
- consoleSandbox ,
7
6
generateTraceId ,
8
7
getClient ,
9
8
getCurrentScope ,
@@ -233,8 +232,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
233
232
...defaultRequestInstrumentationOptions ,
234
233
} ;
235
234
236
- let _hasBeenInitialized = false ;
237
-
238
235
/**
239
236
* The Browser Tracing integration automatically instruments browser pageload/navigation
240
237
* actions as transactions, and captures requests, metrics and errors as spans.
@@ -245,23 +242,17 @@ let _hasBeenInitialized = false;
245
242
* We explicitly export the proper type here, as this has to be extended in some cases.
246
243
*/
247
244
export const browserTracingIntegration = ( ( _options : Partial < BrowserTracingOptions > = { } ) => {
248
- if ( _hasBeenInitialized ) {
249
- consoleSandbox ( ( ) => {
250
- // eslint-disable-next-line no-console
251
- console . warn ( 'Multiple browserTracingIntegration instances are not supported.' ) ;
252
- } ) ;
253
- }
254
-
255
- _hasBeenInitialized = true ;
245
+ const latestRoute : RouteInfo = {
246
+ name : undefined ,
247
+ source : undefined ,
248
+ } ;
256
249
257
250
/**
258
251
* This is just a small wrapper that makes `document` optional.
259
252
* We want to be extra-safe and always check that this exists, to ensure weird environments do not blow up.
260
253
*/
261
254
const optionalWindowDocument = WINDOW . document as ( typeof WINDOW ) [ 'document' ] | undefined ;
262
255
263
- registerSpanErrorInstrumentation ( ) ;
264
-
265
256
const {
266
257
enableInp,
267
258
enableLongTask,
@@ -287,31 +278,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
287
278
..._options ,
288
279
} ;
289
280
290
- const _collectWebVitals = startTrackingWebVitals ( { recordClsStandaloneSpans : enableStandaloneClsSpans || false } ) ;
291
-
292
- if ( enableInp ) {
293
- startTrackingINP ( ) ;
294
- }
295
-
296
- if (
297
- enableLongAnimationFrame &&
298
- GLOBAL_OBJ . PerformanceObserver &&
299
- PerformanceObserver . supportedEntryTypes &&
300
- PerformanceObserver . supportedEntryTypes . includes ( 'long-animation-frame' )
301
- ) {
302
- startTrackingLongAnimationFrames ( ) ;
303
- } else if ( enableLongTask ) {
304
- startTrackingLongTasks ( ) ;
305
- }
306
-
307
- if ( enableInteractions ) {
308
- startTrackingInteractions ( ) ;
309
- }
310
-
311
- const latestRoute : RouteInfo = {
312
- name : undefined ,
313
- source : undefined ,
314
- } ;
281
+ let _collectWebVitals : undefined | ( ( ) => void ) ;
315
282
316
283
/** Create routing idle transaction. */
317
284
function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
@@ -340,7 +307,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
340
307
// should wait for finish signal if it's a pageload transaction
341
308
disableAutoFinish : isPageloadTransaction ,
342
309
beforeSpanEnd : span => {
343
- _collectWebVitals ( ) ;
310
+ _collectWebVitals ?. ( ) ;
344
311
addPerformanceEntries ( span , { recordClsOnPageloadSpan : ! enableStandaloneClsSpans } ) ;
345
312
setActiveIdleSpan ( client , undefined ) ;
346
313
@@ -378,8 +345,29 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
378
345
379
346
return {
380
347
name : BROWSER_TRACING_INTEGRATION_ID ,
381
- afterAllSetup ( client ) {
382
- let startingUrl : string | undefined = getLocationHref ( ) ;
348
+ setup ( client ) {
349
+ registerSpanErrorInstrumentation ( ) ;
350
+
351
+ _collectWebVitals = startTrackingWebVitals ( { recordClsStandaloneSpans : enableStandaloneClsSpans || false } ) ;
352
+
353
+ if ( enableInp ) {
354
+ startTrackingINP ( ) ;
355
+ }
356
+
357
+ if (
358
+ enableLongAnimationFrame &&
359
+ GLOBAL_OBJ . PerformanceObserver &&
360
+ PerformanceObserver . supportedEntryTypes &&
361
+ PerformanceObserver . supportedEntryTypes . includes ( 'long-animation-frame' )
362
+ ) {
363
+ startTrackingLongAnimationFrames ( ) ;
364
+ } else if ( enableLongTask ) {
365
+ startTrackingLongTasks ( ) ;
366
+ }
367
+
368
+ if ( enableInteractions ) {
369
+ startTrackingInteractions ( ) ;
370
+ }
383
371
384
372
function maybeEndActiveSpan ( ) : void {
385
373
const activeSpan = getActiveIdleSpan ( client ) ;
@@ -440,6 +428,9 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
440
428
...startSpanOptions ,
441
429
} ) ;
442
430
} ) ;
431
+ } ,
432
+ afterAllSetup ( client ) {
433
+ let startingUrl : string | undefined = getLocationHref ( ) ;
443
434
444
435
if ( linkPreviousTrace !== 'off' ) {
445
436
linkTraces ( client , { linkPreviousTrace, consistentTraceSampling } ) ;
0 commit comments