@@ -3,7 +3,6 @@ import type { Client, IntegrationFn, Span, StartSpanOptions, TransactionSource,
33import {
44 addNonEnumerableProperty ,
55 browserPerformanceTimeOrigin ,
6- consoleSandbox ,
76 generateTraceId ,
87 getClient ,
98 getCurrentScope ,
@@ -233,8 +232,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
233232 ...defaultRequestInstrumentationOptions ,
234233} ;
235234
236- let _hasBeenInitialized = false ;
237-
238235/**
239236 * The Browser Tracing integration automatically instruments browser pageload/navigation
240237 * actions as transactions, and captures requests, metrics and errors as spans.
@@ -245,23 +242,17 @@ let _hasBeenInitialized = false;
245242 * We explicitly export the proper type here, as this has to be extended in some cases.
246243 */
247244export 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+ } ;
256249
257250 /**
258251 * This is just a small wrapper that makes `document` optional.
259252 * We want to be extra-safe and always check that this exists, to ensure weird environments do not blow up.
260253 */
261254 const optionalWindowDocument = WINDOW . document as ( typeof WINDOW ) [ 'document' ] | undefined ;
262255
263- registerSpanErrorInstrumentation ( ) ;
264-
265256 const {
266257 enableInp,
267258 enableLongTask,
@@ -287,31 +278,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
287278 ..._options ,
288279 } ;
289280
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 ) ;
315282
316283 /** Create routing idle transaction. */
317284 function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
@@ -340,7 +307,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
340307 // should wait for finish signal if it's a pageload transaction
341308 disableAutoFinish : isPageloadTransaction ,
342309 beforeSpanEnd : span => {
343- _collectWebVitals ( ) ;
310+ _collectWebVitals ?. ( ) ;
344311 addPerformanceEntries ( span , { recordClsOnPageloadSpan : ! enableStandaloneClsSpans } ) ;
345312 setActiveIdleSpan ( client , undefined ) ;
346313
@@ -378,8 +345,29 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
378345
379346 return {
380347 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+ }
383371
384372 function maybeEndActiveSpan ( ) : void {
385373 const activeSpan = getActiveIdleSpan ( client ) ;
@@ -440,6 +428,9 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
440428 ...startSpanOptions ,
441429 } ) ;
442430 } ) ;
431+ } ,
432+ afterAllSetup ( client ) {
433+ let startingUrl : string | undefined = getLocationHref ( ) ;
443434
444435 if ( linkPreviousTrace !== 'off' ) {
445436 linkTraces ( client , { linkPreviousTrace, consistentTraceSampling } ) ;
0 commit comments