Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(react): Add POP guard for long-running pageload spans#17867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chargome
merged 8 commits into
develop
from
onur/react-router-long-running-pageload-guardOct 15, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
249062e
fix(react): Add `POP` guard for long-running `pageload` spans
onurtemizkan af57095
Improve
onurtemizkan 52740b9
Make memory router consistent
onurtemizkan 9ac974d
Improve the loading state checks
onurtemizkan 1ba305f
Add E2E tests
onurtemizkan 8576462
Stop relying on `idle` state
onurtemizkan 851b1b0
Make it more defensive
onurtemizkan 1fb8a6d
Address potential race condition
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6 dev-packages/e2e-tests/test-applications/react-router-7-lazy-routes/src/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4 dev-packages/e2e-tests/test-applications/react-router-7-lazy-routes/src/pages/Index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49 ...2e-tests/test-applications/react-router-7-lazy-routes/src/pages/LongRunningLazyRoutes.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { Link, useParams } from 'react-router-dom'; | ||
| // Component that simulates a long-running component load | ||
| // This is used to test the POP guard during long-running pageloads | ||
| const SlowLoadingComponent = () => { | ||
| const { id } = useParams<{ id: string }>(); | ||
| const [data, setData] = useState<string | null>(null); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
| useEffect(() => { | ||
| // Simulate a component that takes time to initialize | ||
| // This extends the pageload duration to create a window where POP events might occur | ||
| setTimeout(() => { | ||
| setData(`Data loaded for ID: ${id}`); | ||
| setIsLoading(false); | ||
| }, 1000); | ||
| }, [id]); | ||
| if (isLoading) { | ||
| return <div id="loading-indicator">Loading...</div>; | ||
| } | ||
| return ( | ||
| <div id="slow-loading-content"> | ||
| <div>{data}</div> | ||
| <Link to="/" id="navigate-home"> | ||
| Go Home | ||
| </Link> | ||
| </div> | ||
| ); | ||
| }; | ||
| export const longRunningNestedRoutes = [ | ||
| { | ||
| path: 'slow', | ||
| children: [ | ||
| { | ||
| path: ':id', | ||
| element: <SlowLoadingComponent />, | ||
| loader: async () => { | ||
| // Simulate slow data fetching in the loader | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); | ||
| return null; | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; |
83 changes: 83 additions & 0 deletions
83 ...ackages/e2e-tests/test-applications/react-router-7-lazy-routes/tests/transactions.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 106 additions & 28 deletions
134 packages/react/src/reactrouter-compat-utils/instrumentation.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -241,6 +241,12 @@ export function createV6CompatibleWrapCreateBrowserRouter< | ||
| const activeRootSpan = getActiveRootSpan(); | ||
| // Track whether we've completed the initial pageload to properly distinguish | ||
| // between POPs that occur during pageload vs. legitimate back/forward navigation. | ||
| let isInitialPageloadComplete = false; | ||
| let hasSeenPageloadSpan = !!activeRootSpan && spanToJSON(activeRootSpan).op === 'pageload'; | ||
| let hasSeenPopAfterPageload = false; | ||
| // The initial load ends when `createBrowserRouter` is called. | ||
| // This is the earliest convenient time to update the transaction name. | ||
| // Callbacks to `router.subscribe` are not called for the initial load. | ||
| @@ -255,20 +261,31 @@ export function createV6CompatibleWrapCreateBrowserRouter< | ||
| } | ||
| router.subscribe((state: RouterState) => { | ||
| if (state.historyAction === 'PUSH' || state.historyAction === 'POP') { | ||
| // Wait for the next render if loading an unsettled route | ||
| if (state.navigation.state !== 'idle') { | ||
| requestAnimationFrame(() => { | ||
| handleNavigation({ | ||
| location: state.location, | ||
| routes, | ||
| navigationType: state.historyAction, | ||
| version, | ||
| basename, | ||
| allRoutes: Array.from(allRoutes), | ||
| }); | ||
| }); | ||
| } else { | ||
| // Track pageload completion to distinguish POPs during pageload from legitimate back/forward navigation | ||
| if (!isInitialPageloadComplete) { | ||
| const currentRootSpan = getActiveRootSpan(); | ||
| const isCurrentlyInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload'; | ||
| if (isCurrentlyInPageload) { | ||
| hasSeenPageloadSpan = true; | ||
| } else if (hasSeenPageloadSpan) { | ||
| // Pageload span was active but is now gone - pageload has completed | ||
| if (state.historyAction === 'POP' && !hasSeenPopAfterPageload) { | ||
| // Pageload ended: ignore the first POP after pageload | ||
| hasSeenPopAfterPageload = true; | ||
| } else { | ||
| // Pageload ended: either non-POP action or subsequent POP | ||
| isInitialPageloadComplete = true; | ||
| } | ||
| } | ||
| // If we haven't seen a pageload span yet, keep waiting (don't mark as complete) | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const shouldHandleNavigation = | ||
| state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete); | ||
| if (shouldHandleNavigation) { | ||
| const navigationHandler = (): void => { | ||
| handleNavigation({ | ||
| location: state.location, | ||
| routes, | ||
| @@ -277,6 +294,13 @@ export function createV6CompatibleWrapCreateBrowserRouter< | ||
| basename, | ||
| allRoutes: Array.from(allRoutes), | ||
| }); | ||
| }; | ||
| // Wait for the next render if loading an unsettled route | ||
| if (state.navigation.state !== 'idle') { | ||
| requestAnimationFrame(navigationHandler); | ||
| } else { | ||
| navigationHandler(); | ||
| } | ||
| } | ||
| }); | ||
| @@ -327,7 +351,6 @@ export function createV6CompatibleWrapCreateMemoryRouter< | ||
| const router = createRouterFunction(routes, wrappedOpts); | ||
| const basename = opts?.basename; | ||
| const activeRootSpan = getActiveRootSpan(); | ||
| let initialEntry = undefined; | ||
| const initialEntries = opts?.initialEntries; | ||
| @@ -348,21 +371,68 @@ export function createV6CompatibleWrapCreateMemoryRouter< | ||
| : initialEntry | ||
| : router.state.location; | ||
| if (router.state.historyAction === 'POP' && activeRootSpan) { | ||
| updatePageloadTransaction({ activeRootSpan, location, routes, basename, allRoutes: Array.from(allRoutes) }); | ||
| const memoryActiveRootSpan = getActiveRootSpan(); | ||
| if (router.state.historyAction === 'POP' && memoryActiveRootSpan) { | ||
| updatePageloadTransaction({ | ||
| activeRootSpan: memoryActiveRootSpan, | ||
| location, | ||
| routes, | ||
| basename, | ||
| allRoutes: Array.from(allRoutes), | ||
| }); | ||
| } | ||
| // Track whether we've completed the initial pageload to properly distinguish | ||
| // between POPs that occur during pageload vs. legitimate back/forward navigation. | ||
| let isInitialPageloadComplete = false; | ||
| let hasSeenPageloadSpan = !!memoryActiveRootSpan && spanToJSON(memoryActiveRootSpan).op === 'pageload'; | ||
| let hasSeenPopAfterPageload = false; | ||
| router.subscribe((state: RouterState) => { | ||
| // Track pageload completion to distinguish POPs during pageload from legitimate back/forward navigation | ||
| if (!isInitialPageloadComplete) { | ||
| const currentRootSpan = getActiveRootSpan(); | ||
| const isCurrentlyInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload'; | ||
| if (isCurrentlyInPageload) { | ||
| hasSeenPageloadSpan = true; | ||
| } else if (hasSeenPageloadSpan) { | ||
| // Pageload span was active but is now gone - pageload has completed | ||
| if (state.historyAction === 'POP' && !hasSeenPopAfterPageload) { | ||
| // Pageload ended: ignore the first POP after pageload | ||
| hasSeenPopAfterPageload = true; | ||
| } else { | ||
| // Pageload ended: either non-POP action or subsequent POP | ||
| isInitialPageloadComplete = true; | ||
| } | ||
| } | ||
| // If we haven't seen a pageload span yet, keep waiting (don't mark as complete) | ||
| } | ||
| const location = state.location; | ||
| if (state.historyAction === 'PUSH' || state.historyAction === 'POP') { | ||
| handleNavigation({ | ||
| location, | ||
| routes, | ||
| navigationType: state.historyAction, | ||
| version, | ||
| basename, | ||
| allRoutes: Array.from(allRoutes), | ||
| }); | ||
| const shouldHandleNavigation = | ||
| state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete); | ||
| if (shouldHandleNavigation) { | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const navigationHandler = (): void => { | ||
| handleNavigation({ | ||
| location, | ||
| routes, | ||
| navigationType: state.historyAction, | ||
| version, | ||
| basename, | ||
| allRoutes: Array.from(allRoutes), | ||
| }); | ||
| }; | ||
| // Wait for the next render if loading an unsettled route | ||
| if (state.navigation.state !== 'idle') { | ||
| requestAnimationFrame(navigationHandler); | ||
| } else { | ||
| navigationHandler(); | ||
| } | ||
| } | ||
| }); | ||
| @@ -532,8 +602,16 @@ function wrapPatchRoutesOnNavigation( | ||
| // Update navigation span after routes are patched | ||
| const activeRootSpan = getActiveRootSpan(); | ||
| if (activeRootSpan && (spanToJSON(activeRootSpan) as { op?: string }).op === 'navigation') { | ||
| // For memory routers, we should not access window.location; use targetPath only | ||
| const pathname = isMemoryRouter ? targetPath : targetPath || WINDOW.location?.pathname; | ||
| // Determine pathname based on router type | ||
| let pathname: string | undefined; | ||
| if (isMemoryRouter) { | ||
| // For memory routers, only use targetPath | ||
| pathname = targetPath; | ||
| } else { | ||
| // For browser routers, use targetPath or fall back to window.location | ||
| pathname = targetPath || WINDOW.location?.pathname; | ||
| } | ||
| if (pathname) { | ||
| updateNavigationSpan( | ||
| activeRootSpan, | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.