Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions packages/react/src/reactrouter-compat-utils/instrumentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ 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 isPageloadComplete = 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.
Expand All @@ -255,20 +259,23 @@ 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 {
const currentRootSpan = getActiveRootSpan();
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';

// Mark pageload as complete once we're no longer in a pageload span
if (!isStillInPageload && !isPageloadComplete) {
isPageloadComplete = true;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Race Condition in Pageload Completion Logic

The pageload completion logic has a race condition. It can prematurely mark pageload as complete if the root span is missing, not necessarily because pageload finished. This causes legitimate POP navigation events immediately following pageload to be incorrectly ignored.

Fix in Cursor Fix in Web


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

if (shouldHandleNavigation) {
const navigationHandler = (): void => {
handleNavigation({
location: state.location,
routes,
Expand All @@ -277,6 +284,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();
}
}
});
Expand Down Expand Up @@ -352,9 +366,28 @@ export function createV6CompatibleWrapCreateMemoryRouter<
updatePageloadTransaction({ activeRootSpan, 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 isPageloadComplete = false;

router.subscribe((state: RouterState) => {
const currentRootSpan = getActiveRootSpan();
const isStillInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';

// Mark pageload as complete once we're no longer in a pageload span
if (!isStillInPageload && !isPageloadComplete) {
isPageloadComplete = true;
}

const location = state.location;
if (state.historyAction === 'PUSH' || state.historyAction === 'POP') {
const shouldHandleNavigation =
state.historyAction === 'PUSH' ||
// Only handle POP events after pageload is complete.
// This prevents creating navigation spans for POP events during long-running pageloads,
// while still allowing legitimate back/forward button navigation to be tracked.
(state.historyAction === 'POP' && isPageloadComplete);

if (shouldHandleNavigation) {
handleNavigation({
location,
routes,
Expand Down
Loading