fix(router-core): don't resolve aborted match to success without loaderData #7673
main fromrefs/pull/7673/head·opened Summary#
A route's component only renders when its match has status === 'success', which is exactly why useLoaderData() is typed as non-undefined. However, when a loader is aborted mid-flight before producing data, the match was promoted from pending to success without loaderData being set. The component then rendered while useLoaderData() returned undefined at runtime, despite the type guaranteeing a value — leading to crashes such as Cannot read properties of undefined.
Root cause#
The AbortError branch in runLoader (packages/router-core/src/load-matches.ts) did:
status: prev.status === 'pending' ? 'success' : prev.status
For a first load (pending, no data yet) this fabricated a success state with loaderData === undefined, breaking the invariant that a successful match always carries loader data. This branch was introduced in #4570 to avoid flashing the error boundary on a spontaneous abort.
Fix#
In the AbortError branch, the three cases are now distinguished:
| Case | Behavior |
|---|---|
The match's own abortController fired (rapid navigation) | Bail out without mutating state (unchanged, from #6426) |
| The loader aborted but a previous load already produced data | Keep showing it — stale-while-revalidate (unchanged) |
The loader aborted with no data yet (pending) | Fall through to the normal error handling → status: 'error' (renders the errorComponent) |
This restores the invariant success ⟹ loaderData defined, so useLoaderData() inside a route component is genuinely non-undefined at both the type and runtime level.
Behavior change#
Previously a loader that threw AbortError on initial load rendered the route component with loaderData undefined (see #4570). It now renders the errorComponent instead — the designed mechanism for "no data available." The rapid-navigation abort path (#6426, reproducer for #6388) is unaffected, and stale-while-revalidate is preserved.
Tests#
- Updated
throw abortError from loader upon initial load with basepathin bothreact-routerandsolid-routerto assert the error boundary renders instead of the component. - Added
aborted loader does not render the route component with undefined loaderDatain bothreact-routerandsolid-router, where the component readsloaderData. - Full unit + type suites pass for
router-core,react-router,solid-router, andvue-router; eslint is clean.
Summary by CodeRabbit#
- Bug Fixes
- Fixed the behavior of aborted loaders to properly resolve as errors. Routes with interrupted loaders now correctly display the error component instead of rendering with undefined loader data.
- 1
closing in favor of https://github.com/TanStack/router/pull/7637
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
c1acb4a3-3f7b-45bb-aabf-8bb456d60c2e📥 Commits
Reviewing files that changed from the base of the PR and between 279a8499162c6229665d26826eb7f7dfc5428cb4 and 8ec67d4435b627bb0e292e425607f95fd0fdd62f.
📒 Files selected for processing (4)
.changeset/fix-aborted-loader-undefined-loaderdata.mdpackages/react-router/tests/loaders.test.tsxpackages/router-core/src/load-matches.tspackages/solid-router/tests/loaders.test.tsx📝 Walkthrough
Walkthrough#
The
runLoaderabort handler inload-matches.tsis changed so that when anAbortErroris caught and the match is stillpending(no loader data produced), it falls through to normal error handling instead of forcing asuccessstate. Existing loader tests and a new regression test in bothreact-routerandsolid-routerverify theerrorComponentrenders and the route component does not.Changes#
Aborted Loader Error Resolution Fix
runLoaderpackages/router-core/src/load-matches.tspending, the code no longer unconditionally transitions tosuccess. Non-pending matches preserve existing data by only clearingisFetching; pending matches fall through to the error path.packages/react-router/tests/loaders.test.tsx,packages/solid-router/tests/loaders.test.tsx,.changeset/fix-aborted-loader-undefined-loaderdata.mderrorComponentoutput and absence of route content. New regression tests cover the case where a loader rejects withAbortErrorbefore producing data. Changeset documents the@tanstack/router-corepatch.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Suggested labels#
package: react-router,package: router-corePoem#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share