@@ -241,6 +241,10 @@ export function createV6CompatibleWrapCreateBrowserRouter<
241
241
242
242
const activeRootSpan = getActiveRootSpan ( ) ;
243
243
244
+ // Track whether we've handled the initial POP to avoid creating navigation spans
245
+ // for POP events that occur during the initial pageload phase.
246
+ let hasHandledInitialPop = false ;
247
+
244
248
// The initial load ends when `createBrowserRouter` is called.
245
249
// This is the earliest convenient time to update the transaction name.
246
250
// Callbacks to `router.subscribe` are not called for the initial load.
@@ -252,23 +256,19 @@ export function createV6CompatibleWrapCreateBrowserRouter<
252
256
basename,
253
257
allRoutes : Array . from ( allRoutes ) ,
254
258
} ) ;
259
+ hasHandledInitialPop = true ;
255
260
}
256
261
257
262
router . subscribe ( ( state : RouterState ) => {
258
- if ( state . historyAction === 'PUSH' || state . historyAction === 'POP' ) {
259
- // Wait for the next render if loading an unsettled route
260
- if ( state . navigation . state !== 'idle' ) {
261
- requestAnimationFrame ( ( ) => {
262
- handleNavigation ( {
263
- location : state . location ,
264
- routes,
265
- navigationType : state . historyAction ,
266
- version,
267
- basename,
268
- allRoutes : Array . from ( allRoutes ) ,
269
- } ) ;
270
- } ) ;
271
- } else {
263
+ const shouldHandleNavigation =
264
+ state . historyAction === 'PUSH' ||
265
+ // Only handle POP events after the initial pageload POP has been processed.
266
+ // This prevents creating navigation spans for POP events during long-running pageloads,
267
+ // while still allowing legitimate back/forward button navigation to be tracked.
268
+ ( state . historyAction === 'POP' && hasHandledInitialPop ) ;
269
+
270
+ if ( shouldHandleNavigation ) {
271
+ const navigationHandler = ( ) : void => {
272
272
handleNavigation ( {
273
273
location : state . location ,
274
274
routes,
@@ -277,6 +277,13 @@ export function createV6CompatibleWrapCreateBrowserRouter<
277
277
basename,
278
278
allRoutes : Array . from ( allRoutes ) ,
279
279
} ) ;
280
+ } ;
281
+
282
+ // Wait for the next render if loading an unsettled route
283
+ if ( state . navigation . state !== 'idle' ) {
284
+ requestAnimationFrame ( navigationHandler ) ;
285
+ } else {
286
+ navigationHandler ( ) ;
280
287
}
281
288
}
282
289
} ) ;
0 commit comments