fix(router-core): handle AbortError in router execution flow #4570
main fromrefs/pull/4570/head·merged by schiller-manuel(GitHub profile, opens in a new tab) fixes #4517
Summary by CodeRabbit#
Bug Fixes
- Prevents error screens when a loader is aborted (AbortError). The app now continues rendering the intended content, applies head updates, and respects base paths on initial load.
- Reduces unnecessary error flashes during canceled navigations.
Tests
- Added tests validating AbortError handling on initial load with a base path for React and Solid routers.
- Minor test update adjusting className order to stabilize assertions.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
More 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@4570@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@4570@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@4570@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@4570@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@4570@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@4570@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@4570@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@4570@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@4570@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@4570@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@4570@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@4570@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@4570@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@4570@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@4570@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@4570@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@4570@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@4570@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@4570@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@4570@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@4570@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@4570@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@4570@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@4570@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@4570@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@4570@tanstack/start-fn-stubs
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@4570@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@4570@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@4570@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@4570@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@4570@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@4570@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@4570@tanstack/vue-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@4570@tanstack/vue-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@4570@tanstack/vue-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@4570@tanstack/vue-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@4570@tanstack/vue-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@4570@tanstack/vue-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@4570@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@4570commit:
7d62f47- @@ -2511,3 +2511,19 @@25112511 ...head,25122512 }))25132513 } catch (e) {2514+ if (2515+ e instanceof DOMException &&2516+ e.name === 'AbortError'2517+ ) {2518+ const head = await executeHead()2519+ updateMatch(matchId, (prev) => ({2520+ ...prev,2521+ status:2522+ prev.status === 'pending'2523+ ? 'success'2524+ : prev.status,2525+ ...head,2526+ }))2527+ return2528+ }2529+
According to the documentation, the
abortController.abort()method throws anErrorobject of typeDOMExceptionwith the nameAbortError. When this error is thrown during route loading, it is treated as a normal control flow and the route is updated to a valid state accordingly.@SeanCassiere
I noticed you’ve explored similar concerns before(#4531), so I think it would be great to discuss this approach together.
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
@SeanCassiere
Would there be a chance for us to have a conversation about this?
SeanCassiere reviewed
Sorry about that!
@schiller-manuel there were recent changes this flow if I'm not mistaken with the selective SSR stuff. Would this be an issue?
@leesb971204 provided Manuel's fine with these changes, I don't see a reason not to get this pushed in.
Edit: Also, please make sure that your test are reflected in
@tanstack/solid-routeras well.github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
Sorry about that!
@schiller-manuel there were recent changes this flow if I'm not mistaken with the selective SSR stuff. Would this be an issue?
@leesb971204 provided Manuel's fine with these changes, I don't see a reason not to get this pushed in.
Edit: Also, please make sure that your test are reflected in
@tanstack/solid-routeras well.Thanks for the comment! I’ve added the same test to solid-router as well.
Sorry about that!
@schiller-manuel there were recent changes this flow if I'm not mistaken with the selective SSR stuff. Would this be an issue?
@leesb971204 provided Manuel's fine with these changes, I don't see a reason not to get this pushed in.
Edit: Also, please make sure that your test are reflected in
@tanstack/solid-routeras well.Are there any other tasks that need to be completed before this work can be approved?
Are there any other tasks that need to be completed before this work can be approved?
Other than the current merge conflict, as mentioned here, I'd like @schiller-manuel to take a look at how this is being handled.
Is there any particular reason this work hasn’t been reviewed yet?
no, we are just busy with a lot of stuff, sorry. will get back to you as soon as time permits
Walkthrough#
Adds AbortError handling in loader error flow to prevent error propagation on initial load with basepath. Executes head, preserves/updates match status, and returns early. Adds React and Solid tests reproducing AbortError during basepath mount and asserting no error UI and correct path.
Changes#
Cohort / File(s) Summary Core loader error handling packages/router-core/src/load-matches.tsIntercepts DOMException with name "AbortError" during loader errors; runs executeHead, merges head result, preserves prior pending status as success, and exits without triggering error handling. React tests for AbortError + basepath packages/react-router/tests/loaders.test.tsxAdds test for loader rejecting with AbortError on initial load with basepath; asserts index renders, error UI absent, and path includes basepath. Minor className order change in a reproducer div. Solid tests for AbortError + basepath packages/solid-router/tests/loaders.test.tsxAdds analogous test verifying AbortError on initial load with basepath does not render error component and basepath is preserved. Sequence Diagram(s)#
sequenceDiagram autonumber actor User participant App participant Router participant RouteLoader as Loader participant Head as executeHead participant UI as ErrorBoundary User->>App: Open "/" App->>Router: Initialize with basepath "/app" Router->>Loader: Run loader (initial load) note right of Loader: Abort due to basepath redirect Loader-->>Router: Throw DOMException(name="AbortError") alt AbortError Router->>Head: executeHead() Head-->>Router: headResult note over Router: Merge head, preserve/resolve match status Router-->>App: Render route content (no error) App-->>User: UI shows index under "/app" else Other errors Router->>UI: Propagate to error boundary UI-->>User: Error UI endEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs#
- TanStack/router#4961 — Adjusts loader/error/executeHead interplay in router-core, touching the same loadMatches error pathway.
Suggested reviewers#
- schiller-manuel
Poem#
A hop, a skip, the loaders race— Abort! says basepath, shifting place. No burrowed error, calm instead, We nibble greens while running head. The trail now starts at “/app”, hooray— A rabbit routes the tidy way. 🐇✨
Pre-merge checks and finishing touches#
✅ Passed checks (5 passed)
Check name Status Explanation Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled. Title Check ✅ Passed The title "fix(router-core): handle AbortError in router execution flow" succinctly and accurately describes the primary change (special-casing AbortError handling in router-core) and aligns with the modified files and added tests that target loader abort behavior; it is specific, concise, and relevant to the changeset. Linked Issues Check ✅ Passed The changes directly address issue #4517 by adding special handling for DOMException 'AbortError' in packages/router-core/src/load-matches.ts to prevent the AbortError from bubbling into the ErrorBoundary, and by adding tests in react-router and solid-router that reproduce the basepath initial-load abort scenario and assert the app renders without showing the error route. These code and test updates match the linked issue's objective to avoid treating an unreasoned abort as an application error. Out of Scope Changes Check ✅ Passed All modifications are limited to router-core logic and router-specific tests (react-router and solid-router) that validate the abort behavior; there are no changes to public/exported APIs or unrelated packages in the provided summary. Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped. ✨ Finishing touches
- 📝 Generate Docstrings
🧪 Generate unit tests
- Create PR with unit tests
- Post copyable unit tests in a comment
Comment `@coderabbitai help` to get the list of available commands and usage tips.Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between c2f732d30f53cb6c9143a41a2bff200e7bd5ba80 and d39722e92404860c5f49b0a0b9a1629b5a38731d.
📒 Files selected for processing (3)
packages/react-router/tests/loaders.test.tsx(2 hunks)packages/router-core/src/load-matches.ts(1 hunks)packages/solid-router/tests/loaders.test.tsx(1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
packages/router-core/src/load-matches.tspackages/solid-router/tests/loaders.test.tsxpackages/react-router/tests/loaders.test.tsx
packages/router-core/**
📄 CodeRabbit inference engine (AGENTS.md)
Keep framework-agnostic core router logic in packages/router-core/
Files:
packages/router-core/src/load-matches.ts
packages/{react-router,solid-router}/**
📄 CodeRabbit inference engine (AGENTS.md)
Implement React and Solid bindings/components only in packages/react-router/ and packages/solid-router/
Files:
packages/solid-router/tests/loaders.test.tsxpackages/react-router/tests/loaders.test.tsx
🧬 Code graph analysis (1)
packages/react-router/tests/loaders.test.tsx (1)
packages/react-router/src/router.ts (1)
createRouter(80-82)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (3)
packages/react-router/tests/loaders.test.tsx (2)
433-433: Non-functional className reorder — OKNo behavior change; safe.
733-757: Good coverage for AbortError + basepathTest correctly asserts no error UI and basepath prefix.
Optionally also assert
router.state.location.href.startsWith('/app')for stronger verification.packages/solid-router/tests/loaders.test.tsx (1)
321-345: Solid test parity — looks goodMirrors React test; assertions are appropriate.
Optionally assert
router.state.location.href.startsWith('/app')for symmetry with the React suite.@@ -670,3 +670,13 @@670670 } catch (e) {671671 let error = e672672673+ if (error instanceof DOMException && error.name === 'AbortError') {674+ const head = await executeHead(inner, matchId, route)675+ inner.updateMatch(matchId, (prev) => ({676+ ...prev,677+ status: prev.status === 'pending' ? 'success' : prev.status,678+ ...head,679+ }))680+ return681+ }682+⚠️ Potential issue
DOMException check can crash in SSR; also ensure pending-min duration and reset isFetching on async path
error instanceof DOMExceptionwill throw ifDOMExceptionis undefined (some SSR/envs).- Early return skips awaiting
minPendingPromise, potentially violating pendingMinMs guarantees. - In background reloads (
loaderShouldRunAsync),isFetchingcan remain'loader'after abort since onlyrunLoadercan flip it tofalse.
Fix by detecting abort via
name === 'AbortError', awaiting min-pending, and explicitly settingisFetching: false.- if (error instanceof DOMException && error.name === 'AbortError') { - const head = await executeHead(inner, matchId, route) - inner.updateMatch(matchId, (prev) => ({ - ...prev, - status: prev.status === 'pending' ? 'success' : prev.status, - ...head, - })) - return - } + // Treat aborted loaders as cancellations, not errors + if ((error as any)?.name === 'AbortError') { + const head = await executeHead(inner, matchId, route) + const pendingPromise = match._nonReactive.minPendingPromise + if (pendingPromise) await pendingPromise + inner.updateMatch(matchId, (prev) => ({ + ...prev, + status: prev.status === 'pending' ? 'success' : prev.status, + isFetching: false, + ...head, + })) + return + }📝 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 change673- if (error instanceof DOMException && error.name === 'AbortError') {674- const head = await executeHead(inner, matchId, route)675- inner.updateMatch(matchId, (prev) => ({676- ...prev,677- status: prev.status === 'pending' ? 'success' : prev.status,678- ...head,679- }))680- return681- }682-673+ // Treat aborted loaders as cancellations, not errors674+ if ((error as any)?.name === 'AbortError') {675+ const head = await executeHead(inner, matchId, route)676+ const pendingPromise = match._nonReactive.minPendingPromise677+ if (pendingPromise) await pendingPromise678+ inner.updateMatch(matchId, (prev) => ({679+ ...prev,680+ status: prev.status === 'pending' ? 'success' : prev.status,681+ isFetching: false,682+ ...head,683+ }))684+ return685+ }🤖 Prompt for AI Agents
In packages/router-core/src/load-matches.ts around lines 673–682, replace the fragile `error instanceof DOMException && error.name === 'AbortError'` check with a safe abort detection using `error?.name === 'AbortError'` (or equivalent null-safe check) so SSR environments without DOMException don't throw; before returning, await the existing min-pending promise used elsewhere (e.g., `await minPendingPromise`) to honor pendingMinMs guarantees; and call `inner.updateMatch(matchId, ...)` to explicitly set `isFetching: false` (and keep the current status transition logic: set status to 'success' only if previously 'pending') so background loader aborts don't leave `isFetching` stuck as 'loader'.The tests can only pass after #5202 has been merged.
any update please?
any update please?
According this comment, #5202 must be merged first.
leesb971204(GitHub profile, opens in a new tab) closed this
leesb971204(GitHub profile, opens in a new tab) reopened this
- @@ -730,3 +730,5 @@730730 expect(fooPendingComponentOnMountMock).not.toHaveBeenCalled()731731 })732732733+test('throw abortError from loader upon initial load with basepath', async () => {734+ window.history.replaceState(null, 'root', '/app')
pretend
vite.config.ts'sbase: 'app' @SeanCassiere @schiller-manuel Would anyone be available to review this?
- @@ -699,3 +699,4 @@699699 } catch (e) {700700 let error = e701701702+ if (error instanceof DOMException && error.name === 'AbortError') {
this should do it:
- SSR safe
- no head execution necessary here
if ((error as any)?.name === 'AbortError') { inner.updateMatch(matchId, (prev) => ({ ...prev, status: prev.status === 'pending' ? 'success' : prev.status, isFetching: false, })) return } @schiller-manuel Thank you for the feedback! I've applied the changes you suggested — whenever you have a moment, please take another look.
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 7d62f47ed1635acb85f99a63cf27167d154eaaee
nx affected --targets=test:eslint,test:unit,tes...nx run-many --target=build --exclude=examples/*...☁️ Nx Cloud last updated this comment at
2026-01-07 00:53:09UTC