@@ -24,7 +24,6 @@ import {
24
24
getDynamicSamplingContextFromSpan ,
25
25
getIsolationScope ,
26
26
getLocationHref ,
27
- getRootSpan ,
28
27
logger ,
29
28
propagationContextFromHeaders ,
30
29
registerSpanErrorInstrumentation ,
@@ -276,6 +275,19 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
276
275
_collectWebVitals ( ) ;
277
276
addPerformanceEntries ( span , { recordClsOnPageloadSpan : ! enableStandaloneClsSpans } ) ;
278
277
setActiveIdleSpan ( client , undefined ) ;
278
+
279
+ // A trace should stay consistent over the entire timespan of one route - even after the pageload/navigation ended.
280
+ // Only when another navigation happens, we want to create a new trace.
281
+ // This way, e.g. errors that occur after the pageload span ended are still associated to the pageload trace.
282
+ const scope = getCurrentScope ( ) ;
283
+ const oldPropagationContext = scope . getPropagationContext ( ) ;
284
+
285
+ scope . setPropagationContext ( {
286
+ ...oldPropagationContext ,
287
+ traceId : idleSpan . spanContext ( ) . traceId ,
288
+ sampled : spanIsSampled ( idleSpan ) ,
289
+ dsc : getDynamicSamplingContextFromSpan ( span ) ,
290
+ } ) ;
279
291
} ,
280
292
} ) ;
281
293
setActiveIdleSpan ( client , idleSpan ) ;
@@ -344,27 +356,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
344
356
} ) ;
345
357
} ) ;
346
358
347
- // A trace should to stay the consistent over the entire time span of one route.
348
- // Therefore, when the initial pageload or navigation root span ends, we update the
349
- // scope's propagation context to keep span-specific attributes like the `sampled` decision and
350
- // the dynamic sampling context valid, even after the root span has ended.
351
- // This ensures that the trace data is consistent for the entire duration of the route.
352
- client . on ( 'spanEnd' , span => {
353
- const op = spanToJSON ( span ) . op ;
354
- if ( span !== getRootSpan ( span ) || ( op !== 'navigation' && op !== 'pageload' ) ) {
355
- return ;
356
- }
357
-
358
- const scope = getCurrentScope ( ) ;
359
- const oldPropagationContext = scope . getPropagationContext ( ) ;
360
-
361
- scope . setPropagationContext ( {
362
- ...oldPropagationContext ,
363
- sampled : oldPropagationContext . sampled !== undefined ? oldPropagationContext . sampled : spanIsSampled ( span ) ,
364
- dsc : oldPropagationContext . dsc || getDynamicSamplingContextFromSpan ( span ) ,
365
- } ) ;
366
- } ) ;
367
-
368
359
if ( WINDOW . location ) {
369
360
if ( instrumentPageLoad ) {
370
361
const origin = browserPerformanceTimeOrigin ( ) ;
0 commit comments