Skip to content

Commit 59e8f76

Browse files
committed
Move idle check to shouldHandleNavigation
1 parent f679c8d commit 59e8f76

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

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

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,22 @@ export function createV6CompatibleWrapCreateBrowserRouter<
277277
// If we haven't seen a pageload span yet, keep waiting (don't mark as complete)
278278
}
279279

280+
// Only handle navigation when it's complete (state is idle).
281+
// During 'loading' or 'submitting', state.location may still have the old pathname,
282+
// which would cause us to create a span for the wrong route.
280283
const shouldHandleNavigation =
281-
state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete);
284+
(state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete)) &&
285+
state.navigation.state === 'idle';
282286

283287
if (shouldHandleNavigation) {
284-
// Only handle navigation when it's complete (state is idle).
285-
// During 'loading' or 'submitting', state.location may still have the old pathname,
286-
// which would cause us to create a span for the wrong route.
287-
if (state.navigation.state === 'idle') {
288-
handleNavigation({
289-
location: state.location,
290-
routes,
291-
navigationType: state.historyAction,
292-
version,
293-
basename,
294-
allRoutes: Array.from(allRoutes),
295-
});
296-
}
288+
handleNavigation({
289+
location: state.location,
290+
routes,
291+
navigationType: state.historyAction,
292+
version,
293+
basename,
294+
allRoutes: Array.from(allRoutes),
295+
});
297296
}
298297
});
299298

@@ -402,29 +401,22 @@ export function createV6CompatibleWrapCreateMemoryRouter<
402401
// If we haven't seen a pageload span yet, keep waiting (don't mark as complete)
403402
}
404403

405-
const location = state.location;
406-
404+
// Only handle navigation when it's complete (state is idle).
405+
// During 'loading' or 'submitting', state.location may still have the old pathname,
406+
// which would cause us to create a span for the wrong route.
407407
const shouldHandleNavigation =
408-
state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete);
408+
(state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete)) &&
409+
state.navigation.state === 'idle';
409410

410411
if (shouldHandleNavigation) {
411-
const navigationHandler = (): void => {
412-
handleNavigation({
413-
location,
414-
routes,
415-
navigationType: state.historyAction,
416-
version,
417-
basename,
418-
allRoutes: Array.from(allRoutes),
419-
});
420-
};
421-
422-
// Wait for the next render if loading an unsettled route
423-
if (state.navigation.state !== 'idle') {
424-
requestAnimationFrame(navigationHandler);
425-
} else {
426-
navigationHandler();
427-
}
412+
handleNavigation({
413+
location: state.location,
414+
routes,
415+
navigationType: state.historyAction,
416+
version,
417+
basename,
418+
allRoutes: Array.from(allRoutes),
419+
});
428420
}
429421
});
430422

0 commit comments

Comments
 (0)