MatchInner can `throw undefined` when a redirected match's loadPromise was already cleared β escapes CatchBoundary and unmounts the whole app #7753
Which project does this relate to?#
Router
Describe the bug#
On initial page load, when an async beforeLoad takes a few hundred ms (in our case: an auth token refresh) and then throws redirect(), there is a race where MatchInner renders a match observed as status === 'redirected' after the redirect navigation has already settled. At that point the match's loadPromise has been cleared, so MatchInner executes throw getMatchPromise(match, 'loadPromise') β which evaluates to throw undefined.
getMatchPromise can return undefined by design of its lookup:
// packages/react-router/src/Match.tsx
const getMatchPromise = (match, key) => {
return (
router.getMatch(match.id)?._nonReactive[key] ?? match._nonReactive[key]
)
}
β¦and router-core clears the promise once loading settles:
// packages/router-core/src/load-matches.ts
match._nonReactive.loadPromise?.resolve()
match._nonReactive.loadPromise = undefined
The thrown undefined is then not contained by CatchBoundary, because its render callback checks truthiness of the error:
// packages/react-router/src/CatchBoundary.tsx
children={({ error, reset }) => {
if (error) { // <- undefined is falsy
return React.createElement(errorComponent, { error, reset })
}
return props.children // <- children re-throw, boundary is considered failed
}}
getDerivedStateFromError(undefined) stores the falsy error, the boundary re-renders its children, they throw again, React treats the boundary as failed and escalates. With React 19 the error reaches the root, the whole tree is unmounted and defaultOnUncaughtError calls reportError(undefined) β the only console output is a stackless Uncaught undefined, and the user gets a permanent white screen (#root is empty).
Steps to reproduce#
Timing-dependent race, so hard to make deterministic, but the shape is:
- SPA with
createRouter(no SSR), React 19,defaultPendingComponentset. - Root-ish route
/whosebeforeLoadawaits something slow (~300β800 ms, e.g. a token refresh request) and then doesthrow redirect({ to: '/somewhere' }). - Cold-load the app at
/. - Intermittently, the app white-screens with
Uncaught undefinedin the console and an empty#root. Reloading (when the slow work is no longer needed, e.g. token now fresh) loads fine.
We hit this consistently-intermittently in production; the window opens whenever the first render commits while the redirect is being processed.
Expected behavior#
Either the stale render suspends/retries (e.g. getMatchPromise falling back to a resolved promise instead of undefined), or at minimum the error boundary contains whatever was thrown β CatchBoundaryImpl tracking a hasError sentinel instead of relying on error truthiness β so a falsy thrown value can't unmount the entire app.
Platform#
- Package: @tanstack/react-router 1.170.17 (also present in current
mainsources) - React 19, client-side only (no SSR)
- Browser: Chrome (any)
Additional context#
Happy to provide more details or test a patch. Workaround we're shipping: an app-level error boundary above RouterProvider that treats falsy thrown values as errors and remounts once.
Sheraff(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
We hit the same failure mode (
MatchInnerthrowing literalundefinedβ escapesCatchBoundaryvia theif (error)truthiness check β React 19 unmounts the whole tree β permanent blank page, stacklessUncaught undefined), but through a second entry path that doesn't involve redirects, so noting it here in case it affects the shape of the fix in #7805.Trigger:
invalidate({ forcePending: true })β pending beforeloadPromiseexists#We call this from an effect after hydrating a prerendered SPA shell, to re-run the root loader on the client:
In
router-core,invalidateflips the match tostatus: "pending"in the store beforeload()runs and attaches_nonReactive.loadPromise:If React flushes a sync re-render inside that window (for us: the store update lands in a microtask and gets flushed via
flushSyncWorkAcrossRoots_implbeforeload()attaches the promise),MatchInnerImpltakes the pending branch:So this issue's title condition ("loadPromise was already cleared") has a sibling: "loadPromise not yet attached". Same
throw undefined, same boundary escape, same full-app unmount.Observations#
Debugger.setPauseOnExceptions('all')+evaluateOnCallFrameat the throw site:{ routeId: '__root__', status: 'pending', errorType: 'undefined' }, with_forcePending/_displayPendingunset β i.e. it reaches thestatus === 'pending'throw, not the_forcePendingone.@tanstack/react-router1.170.18 /@tanstack/router-core1.171.15, React 19.2.4. Reproduces in Vite dev and (by mechanism) applies to production builds too.Workaround#
Dropping
forcePending(a plaininvalidate()keeps the match instatus: "success"with stale loader data while it reloads) removes the pending window entirely and fixed it for us.Since #7861 was closed in favor of the #7805 loader rewrite: it would be great if the rewrite's test coverage includes the
invalidate({ forcePending: true })-during-render window as well, not just the redirect/cleared-promise race. A defense-in-depthhasErrorsentinel inCatchBoundaryImpl(as #7861 proposed) would also keep any future falsy throw from unmounting the entire app.