Skip to content

Commit e1170a5

Browse files
committed
Improve
1 parent 76835d9 commit e1170a5

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

packages/react/src/reactrouter-compat-utils/instrumentation.tsx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ export function createV6CompatibleWrapCreateBrowserRouter<
241241

242242
const activeRootSpan = getActiveRootSpan();
243243

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;
244+
// Track whether we've completed the initial pageload to properly distinguish
245+
// between POPs that occur during pageload vs. legitimate back/forward navigation.
246+
let isPageloadComplete = false;
247247

248248
// The initial load ends when `createBrowserRouter` is called.
249249
// This is the earliest convenient time to update the transaction name.
@@ -256,16 +256,23 @@ export function createV6CompatibleWrapCreateBrowserRouter<
256256
basename,
257257
allRoutes: Array.from(allRoutes),
258258
});
259-
hasHandledInitialPop = true;
260259
}
261260

262261
router.subscribe((state: RouterState) => {
262+
const currentRootSpan = getActiveRootSpan();
263+
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';
264+
265+
// Mark pageload as complete once we're no longer in a pageload span
266+
if (!isStillInPageload && !isPageloadComplete) {
267+
isPageloadComplete = true;
268+
}
269+
263270
const shouldHandleNavigation =
264271
state.historyAction === 'PUSH' ||
265-
// Only handle POP events after the initial pageload POP has been processed.
272+
// Only handle POP events after pageload is complete.
266273
// This prevents creating navigation spans for POP events during long-running pageloads,
267274
// while still allowing legitimate back/forward button navigation to be tracked.
268-
(state.historyAction === 'POP' && hasHandledInitialPop);
275+
(state.historyAction === 'POP' && isPageloadComplete);
269276

270277
if (shouldHandleNavigation) {
271278
const navigationHandler = (): void => {
@@ -359,9 +366,28 @@ export function createV6CompatibleWrapCreateMemoryRouter<
359366
updatePageloadTransaction({ activeRootSpan, location, routes, basename, allRoutes: Array.from(allRoutes) });
360367
}
361368

369+
// Track whether we've completed the initial pageload to properly distinguish
370+
// between POPs that occur during pageload vs. legitimate back/forward navigation.
371+
let isPageloadComplete = false;
372+
362373
router.subscribe((state: RouterState) => {
374+
const currentRootSpan = getActiveRootSpan();
375+
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';
376+
377+
// Mark pageload as complete once we're no longer in a pageload span
378+
if (!isStillInPageload && !isPageloadComplete) {
379+
isPageloadComplete = true;
380+
}
381+
363382
const location = state.location;
364-
if (state.historyAction === 'PUSH' || state.historyAction === 'POP') {
383+
const shouldHandleNavigation =
384+
state.historyAction === 'PUSH' ||
385+
// Only handle POP events after pageload is complete.
386+
// This prevents creating navigation spans for POP events during long-running pageloads,
387+
// while still allowing legitimate back/forward button navigation to be tracked.
388+
(state.historyAction === 'POP' && isPageloadComplete);
389+
390+
if (shouldHandleNavigation) {
365391
handleNavigation({
366392
location,
367393
routes,

0 commit comments

Comments
 (0)