@@ -24,6 +24,7 @@ import {
2424 getDynamicSamplingContextFromSpan ,
2525 getIsolationScope ,
2626 getLocationHref ,
27+ getRootSpan ,
2728 logger ,
2829 propagationContextFromHeaders ,
2930 registerSpanErrorInstrumentation ,
@@ -35,7 +36,6 @@ import { DEBUG_BUILD } from '../debug-build';
3536import { WINDOW } from '../helpers' ;
3637import { registerBackgroundTabDetection } from './backgroundtab' ;
3738import { defaultRequestInstrumentationOptions , instrumentOutgoingRequests } from './request' ;
38- import type { PreviousTraceInfo } from './previousTrace' ;
3939import {
4040 addPreviousTraceSpanLink ,
4141 getPreviousTraceFromSessionStorage ,
@@ -274,19 +274,10 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
274274 source : undefined ,
275275 } ;
276276
277- let previousTraceInfo : PreviousTraceInfo | undefined ;
278- if ( enablePreviousTrace && persistPreviousTrace ) {
279- previousTraceInfo = getPreviousTraceFromSessionStorage ( ) ;
280- }
281-
282277 /** Create routing idle transaction. */
283278 function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
284279 const isPageloadTransaction = startSpanOptions . op === 'pageload' ;
285280
286- if ( enablePreviousTrace && previousTraceInfo ) {
287- previousTraceInfo = addPreviousTraceSpanLink ( previousTraceInfo , startSpanOptions ) ;
288- }
289-
290281 const finalStartSpanOptions : StartSpanOptions = beforeStartSpan
291282 ? beforeStartSpan ( startSpanOptions )
292283 : startSpanOptions ;
@@ -330,16 +321,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
330321 } ) ;
331322 setActiveIdleSpan ( client , idleSpan ) ;
332323
333- if ( enablePreviousTrace ) {
334- previousTraceInfo = {
335- spanContext : idleSpan . spanContext ( ) ,
336- startTimestamp : spanToJSON ( idleSpan ) . start_timestamp ,
337- } ;
338- if ( persistPreviousTrace ) {
339- storePreviousTraceInSessionStorage ( previousTraceInfo ) ;
340- }
341- }
342-
343324 function emitFinish ( ) : void {
344325 if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
345326 client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
@@ -468,6 +449,22 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
468449 shouldCreateSpanForRequest,
469450 enableHTTPTimings,
470451 } ) ;
452+
453+ if ( enablePreviousTrace ) {
454+ let previousTraceInfo = persistPreviousTrace ? getPreviousTraceFromSessionStorage ( ) : undefined ;
455+
456+ client . on ( 'spanStart' , span => {
457+ if ( getRootSpan ( span ) !== span ) {
458+ return ;
459+ }
460+
461+ previousTraceInfo = addPreviousTraceSpanLink ( previousTraceInfo , span ) ;
462+
463+ if ( persistPreviousTrace ) {
464+ storePreviousTraceInSessionStorage ( previousTraceInfo ) ;
465+ }
466+ } ) ;
467+ }
471468 } ,
472469 } ;
473470} ) satisfies IntegrationFn ;
0 commit comments