fix(router-core): fix handle AbortError logic when rapid navigation #6426
main fromrefs/pull/6426/head·merged by nlynzaad(GitHub profile, opens in a new tab) fixes #6388
Summary
- Fixed a bug where rapid navigation between parameterized routes caused errorComponent to render incorrectly
Problem
When rapidly navigating between parameterized routes (e.g., /something/1 → /something/2 → /something/1), the errorComponent was incorrectly triggered with a TypeError.
Root cause: The existing AbortError handler set status: 'success' without setting loaderData. When the component tried to access loaderData, it was undefined, causing a TypeError that was caught by the error boundary.
Solution
Distinguish between two types of AbortError by checking abortController.signal.aborted:
| AbortError Type | signal.aborted | Handling |
|---|---|---|
| Intentional (loader throws AbortError directly) | false | Original behavior (set status to 'success') |
| Navigation abort (cancelMatches triggered) | true | Don't update match state, only resolve loaderPromise |
Additionally, after awaiting a previous loaderPromise, check if the match status is still 'pending'. If so, the previous loader was aborted and a fresh loader needs to run.
Summary by CodeRabbit#
Bug Fixes
- Fixed rapid navigation between parameterized routes so aborted loads no longer trigger error UI and final route renders reliably.
Tests
- Added an end-to-end test reproducer validating rapid param-route navigation does not show error components.
- Updated existing navigation store-update tests to reflect corrected update counts during navigation.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
leesb971204(GitHub profile, opens in a new tab) changed the title
View your CI Pipeline Execution ↗ for commit f2708c18feca165233924f92de91353e29309ddd
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded 1m 58s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 2s View ↗
☁️ Nx Cloud last updated this comment at
2026-02-05 01:37:16UTCMore templates
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-router-react-example-basic-authjs
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-rsc
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-streaming-data-from-server-functions
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
- tanstack-router-solid-example-authenticated-routes
- tanstack-router-solid-example-authenticated-routes-firebase
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-default-search-params
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-basic-virtual-file-based
- tanstack-router-solid-example-basic-virtual-inside-file-based
- tanstack-router-solid-example-deferred-data
- tanstack-router-solid-i18n-paraglide
- tanstack-router-solid-example-kitchen-sink
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-kitchen-sink-solid-query
- tanstack-router-solid-example-kitchen-sink-solid-query-file-based
- tanstack-router-solid-example-large-file-based
- tanstack-router-solid-example-location-masking
- tanstack-router-solid-example-navigation-blocking
- tanstack-router-solid-example-quickstart
- tanstack-router-solid-example-quickstart-esbuild-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-router-solid-example-quickstart-rspack-file-based
- tanstack-router-solid-example-quickstart-webpack-file-based
- @tanstack/router-solid-mono-simple
- router-solid-mono-simple-lazy
- router-solid-monorepo-solid-query
- tanstack-router-solid-example-scroll-restoration
- tanstack-solid-router-search-validator-adapters
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-auth
- tanstack-solid-start-example-basic-authjs
- tanstack-solid-start-example-basic-cloudflare
- tanstack-solid-start-example-basic-netlify
- tanstack-solid-start-example-basic-nitro
- tanstack-start-example-basic-solid-query
- tanstack-solid-start-example-basic-static
- tanstack-solid-start-bun-hosting
- tanstack-solid-start-example-convex-better-auth
- tanstack-solid-start-example-counter
- tanstack-solid-start-i18n-paraglide
- tanstack-solid-start-example-large
- tanstack-solid-start-streaming-data-from-server-functions
- tanstack-solid-start-example-supabase-basic
- tanstack-solid-start-tailwind-v4
- tanstack-router-solid-example-view-transitions
- tanstack-router-solid-example-with-framer-motion
- tanstack-router-solid-example-with-trpc
- tanstack-router-vue-example-basic-jsx
- tanstack-router-vue-example-basic-file-based-jsx
- tanstack-router-vue-example-basic-file-based-sfc
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6426@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6426@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6426@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6426@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6426@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6426@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6426@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6426@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6426@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6426@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6426@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6426@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6426@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6426@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6426@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6426@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6426@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6426@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6426@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6426@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6426@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6426@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6426@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6426@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6426@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6426@tanstack/start-fn-stubs
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6426@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6426@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6426@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6426@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6426@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6426@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6426@tanstack/vue-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6426@tanstack/vue-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6426@tanstack/vue-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6426@tanstack/vue-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6426@tanstack/vue-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6426@tanstack/vue-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6426@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6426commit:
f2708c1Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/router-core/src/load-matches.ts`: - Around line 783-792: In loadRouteMatch ensure any pending timeout set during this load cycle is cleared before the early return: if you created a pendingTimeout (e.g., pendingTimeout or inner.pendingTimeout) clear it with clearTimeout(...) and reset the variable (e.g., pendingTimeout = undefined or inner.pendingTimeout = undefined) immediately before returning when matchAfterWait.status !== 'pending'; do this near the block that reads matchAfterWait and calls handleRedirectAndNotFound so pending timers don't persist and block future pending scheduling.🧹 Nitpick comments (1)
packages/react-router/tests/loaders.test.tsx (1)
828-936: Tighten final-state assertions to avoid false positives.Right now the test passes as long as some param page renders and the loader completes. Consider asserting the final param value and expected loader completion id to make the repro more specific (Line 933-936).
♻️ Suggested test assertion enhancement
const paramPage = await screen.findByTestId('param-page') expect(paramPage).toBeInTheDocument() - expect(loaderCompleteMock).toHaveBeenCalled() + expect(paramPage).toHaveTextContent('Param Component 1') + expect(loaderCompleteMock).toHaveBeenCalledWith('1')packages/router-core/src/load-matches.ts lines 783 to 792OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -773,9 +780,13 @@773780 return prevMatch774781 }775782 await prevMatch._nonReactive.loaderPromise776- const match = inner.router.getMatch(matchId)!777- const error = match._nonReactive.error || match.error783+ const matchAfterWait = inner.router.getMatch(matchId)!784+ const error = matchAfterWait._nonReactive.error || matchAfterWait.error778785 if (error) {779- handleRedirectAndNotFound(inner, match, error)786+ handleRedirectAndNotFound(inner, matchAfterWait, error)780787 }781- } else {788+789+ if (matchAfterWait.status !== 'pending') {790+ return matchAfterWait791+ }792+ }⚠️ Potential issue | 🟠 Major
Clear pendingTimeout before the early return.
The new early return skips the cleanup block at the end of
loadRouteMatch. If a pending timeout was set in this load cycle, it can remain attached and block future pending scheduling. Consider clearing it before returning (Line 789).🧹 Suggested cleanup before return
if (matchAfterWait.status !== 'pending') { + clearTimeout(matchAfterWait._nonReactive.pendingTimeout) + matchAfterWait._nonReactive.pendingTimeout = undefined return matchAfterWait }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change783- const matchAfterWait = inner.router.getMatch(matchId)!784- const error = matchAfterWait._nonReactive.error || matchAfterWait.error785- if (error) {786- handleRedirectAndNotFound(inner, matchAfterWait, error)787- }788-789- if (matchAfterWait.status !== 'pending') {790- return matchAfterWait791- }792- }783+ const matchAfterWait = inner.router.getMatch(matchId)!784+ const error = matchAfterWait._nonReactive.error || matchAfterWait.error785+ if (error) {786+ handleRedirectAndNotFound(inner, matchAfterWait, error)787+ }788+789+ if (matchAfterWait.status !== 'pending') {790+ clearTimeout(matchAfterWait._nonReactive.pendingTimeout)791+ matchAfterWait._nonReactive.pendingTimeout = undefined792+ return matchAfterWait793+ }794+ }🤖 Prompt for AI Agents
In `@packages/router-core/src/load-matches.ts` around lines 783 - 792, In loadRouteMatch ensure any pending timeout set during this load cycle is cleared before the early return: if you created a pendingTimeout (e.g., pendingTimeout or inner.pendingTimeout) clear it with clearTimeout(...) and reset the variable (e.g., pendingTimeout = undefined or inner.pendingTimeout = undefined) immediately before returning when matchAfterWait.status !== 'pending'; do this near the block that reads matchAfterWait and calls handleRedirectAndNotFound so pending timers don't persist and block future pending scheduling.✅ Confirmed as addressed by @nlynzaad
latest commit should have resolved these
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Check the latest commits on this PR branch echo "=== Recent commits ===" git log --oneline -5 echo -e "\n=== Current code around the early return (lines 780-800) ===" sed -n '780,800p' packages/router-core/src/load-matches.ts | cat -n echo -e "\n=== Check for pendingTimeout cleanup before return ===" # Look for the specific pattern of matchAfterWait.status check and what follows grep -A 5 "if (matchAfterWait.status !== 'pending')" packages/router-core/src/load-matches.tsLength of output: 1451
🏁 Script executed:
#!/bin/bash # Let's search for the matchAfterWait variable more broadly echo "=== Searching for matchAfterWait in the file ===" grep -n "matchAfterWait" packages/router-core/src/load-matches.ts echo -e "\n=== Looking at the section where loaderPromise is awaited ===" # Find the await prevMatch._nonReactive.loaderPromise line and show context grep -n -A 15 "await prevMatch._nonReactive.loaderPromise" packages/router-core/src/load-matches.ts echo -e "\n=== Check entire loadRouteMatch function around the relevant section (lines 760-810) ===" sed -n '760,810p' packages/router-core/src/load-matches.ts | cat -nLength of output: 3124
🏁 Script executed:
#!/bin/bash # Let's look at the complete section including what happens after the status check echo "=== Complete section from loaderPromise await through end of else block (lines 820-860) ===" sed -n '820,860p' packages/router-core/src/load-matches.ts | cat -n echo -e "\n=== Check if there's an early return after the status check ===" sed -n '828,845p' packages/router-core/src/load-matches.ts echo -e "\n=== Look at the cleanup section at end of loadRouteMatch ===" grep -n -A 10 "clearTimeout.*pendingTimeout" packages/router-core/src/load-matches.ts | head -20Length of output: 3272
@nlynzaadThank you for the update! I've verified the latest code and confirm the issue is resolved. ✅The implementation took a slightly different approach than my original suggestion:
Current implementation (lines 835-837):
- Conditionally calls
handleLoader()only whenmatch.status === 'pending' - Always falls through to the cleanup section (lines 859-860)
- No early return needed
Original suggestion:
- Would have added an early return when
status !== 'pending' - Required cleanup before the return
Your approach is actually cleaner—by avoiding the early return entirely, the
clearTimeoutat line 859 is always reached regardless of the status, eliminating the risk of lingering timers. Well done! 🎯(_/) (='.'=) (")(")
- Conditionally calls
@schiller-manuel can you please review this?
@nlynzaad can you please review this? I think manuel is too busy now.
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: vue-router
@leesb971204 thanks for this.
I had a look and my only real concern was the change in the control flow on loadRouteMatch.
I've merged with main and made some changes (most just isolating the running of the loader) to address the above.
- packages/solid-router/tests/link.test.tsx line 778Resolvedby nlynzaad(GitHub profile, opens in a new tab)@@ -775,4 +775,4 @@775775 '/posts?page=2&filter=inactive',776776 )777777778- await fireEvent.click(updateSearchLink)778+ fireEvent.click(updateSearchLink)
This isn't a promise so no need to await it
- packages/solid-router/tests/link.test.tsx line 789Resolvedby nlynzaad(GitHub profile, opens in a new tab)@@ -786,5 +786,4 @@786786 const updatedFilter = await screen.findByTestId('current-filter')787787788788 // Verify search was updated789- expect(window.location.pathname).toBe('/posts')790- expect(window.location.search).toBe('?page=2&filter=inactive')789+ // Wait for navigation to complete and search params to update
We should wait for navigation to complete
These are promises and we should wait for these to complete before making assertions
nlynzaad(GitHub profile, opens in a new tab) closed this
@leesb971204 thanks for this.
I had a look and my only real concern was the change in the control flow on loadRouteMatch.
I've merged with main and made some changes (most just isolating the running of the loader) to address the above.
Awesome! Thnks for this!
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Refines loader abort/error handling to distinguish navigation-caused aborts and avoid spurious errorComponent renders; adds an end-to-end test reproducer that rapidly navigates parameterized routes to validate the fix.
Changes#
packages/react-router/tests/loaders.test.tsxpackages/router-core/src/load-matches.tsmatchAfterWait) with an early return if it's no longer pending to prevent stale continuation.packages/react-router/tests/store-updates-during-navigation.test.tsxSequence Diagram(s)#
mermaid sequenceDiagram participant Client participant RouterCore as Router-Core participant Loader as Loader (with AbortController) participant MatchStore as Match State
Client->>RouterCore: navigate to /something/1 RouterCore->>Loader: start loader (attach AbortController) Loader-->>RouterCore: pending result / can be aborted Client->>RouterCore: rapid navigate to /something/2 RouterCore->>Loader: abort previous loader (navigation-caused) RouterCore->>MatchStore: mark previous match as aborted/clear loaderPromise RouterCore->>Loader: start new loader for /something/2 Loader-->>RouterCore: resolves successfully RouterCore->>MatchStore: commit success for latest match (no errorComponent render) Client->>RouterCore: navigate back to /something/1 RouterCore->>Loader: start loader for /something/1 Loader-->>RouterCore: resolves successfully RouterCore->>MatchStore: commit success for latest match (final render)
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~22 minutes
Possibly related PRs#
packages/router-core/src/load-matches.tsloader/match commit and abort behavior.load-matches.ts, affecting similar control flow.Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#6388: distinguishes abort cases, prevents errorComponent rendering, ensures proper match status during rapid navigation, and validates with a comprehensive reproducer test.✨ Finishing touches
Comment `@coderabbitai help` to get the list of available commands and usage tips.