Skip to content

Commit 2e77431

Browse files
committed
Improve the loading state checks
1 parent 965d0ea commit 2e77431

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export function createV6CompatibleWrapCreateBrowserRouter<
243243

244244
// Track whether we've completed the initial pageload to properly distinguish
245245
// between POPs that occur during pageload vs. legitimate back/forward navigation.
246-
let isPageloadComplete = false;
246+
let isInitialPageloadComplete = false;
247247

248248
// The initial load ends when `createBrowserRouter` is called.
249249
// This is the earliest convenient time to update the transaction name.
@@ -259,20 +259,20 @@ export function createV6CompatibleWrapCreateBrowserRouter<
259259
}
260260

261261
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;
262+
// Mark pageload as complete on first PUSH navigation or when pageload span ends
263+
if (state.historyAction === 'PUSH') {
264+
isInitialPageloadComplete = true;
265+
} else if (!isInitialPageloadComplete && state.navigation.state === 'idle') {
266+
const currentRootSpan = getActiveRootSpan();
267+
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';
268+
if (!isStillInPageload) {
269+
isInitialPageloadComplete = true;
270+
return; // Don't handle the POP that completes the initial pageload
271+
}
268272
}
269273

270274
const shouldHandleNavigation =
271-
state.historyAction === 'PUSH' ||
272-
// Only handle POP events after pageload is complete.
273-
// This prevents creating navigation spans for POP events during long-running pageloads,
274-
// while still allowing legitimate back/forward button navigation to be tracked.
275-
(state.historyAction === 'POP' && isPageloadComplete);
275+
state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete);
276276

277277
if (shouldHandleNavigation) {
278278
const navigationHandler = (): void => {
@@ -368,24 +368,24 @@ export function createV6CompatibleWrapCreateMemoryRouter<
368368

369369
// Track whether we've completed the initial pageload to properly distinguish
370370
// between POPs that occur during pageload vs. legitimate back/forward navigation.
371-
let isPageloadComplete = false;
371+
let isInitialPageloadComplete = false;
372372

373373
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;
374+
// Mark pageload as complete on first PUSH navigation or when pageload span ends
375+
if (state.historyAction === 'PUSH') {
376+
isInitialPageloadComplete = true;
377+
} else if (!isInitialPageloadComplete && state.navigation.state === 'idle') {
378+
const currentRootSpan = getActiveRootSpan();
379+
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';
380+
if (!isStillInPageload) {
381+
isInitialPageloadComplete = true;
382+
return; // Don't handle the POP that completes the initial pageload
383+
}
380384
}
381385

382386
const location = state.location;
383387
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);
388+
state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete);
389389

390390
if (shouldHandleNavigation) {
391391
const navigationHandler = (): void => {

0 commit comments

Comments
 (0)