Skip to content

Commit 1c656a5

Browse files
yanghuidongclaude
andcommitted
fix: prevent race condition when navigation changes during head() re-execution
Addresses two scenarios: 1. New navigation starts BEFORE scheduled head() executes 2. New navigation starts WHILE head() is executing Solution: - Capture this navigation's location (thisNavigationLocation) - Before executing head(), check if router's current location matches - If location changed (new navigation), skip stale head() execution - Location objects are always unique (parseLocation creates new objects) Both concerns are resolved: - Scenario 1: Location check prevents stale head() from executing - Scenario 2: Stale head() may complete but fresh navigation overwrites with correct data immediately after 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 8df94c1 commit 1c656a5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/router-core/src/load-matches.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,14 @@ export async function loadMatches(arg: {
959959
if (asyncLoaderPromises.length > 0) {
960960
// Schedule re-execution after all async loaders complete (non-blocking)
961961
// Use allSettled to handle both successful and failed loaders
962-
Promise.allSettled(asyncLoaderPromises).then(() => executeAllHeadFns(inner))
962+
const thisNavigationLocation = inner.location
963+
Promise.allSettled(asyncLoaderPromises).then(() => {
964+
// Only execute if this navigation is still current (not superseded by new navigation)
965+
const latestLocation = inner.router.state.location
966+
if (latestLocation === thisNavigationLocation) {
967+
executeAllHeadFns(inner)
968+
}
969+
})
963970
}
964971

965972
// Throw notFound after head execution

0 commit comments

Comments
 (0)