Skip to content

Commit 8ec67d4

Browse files
committed
fix(router-core): don't resolve aborted match to success without loaderData
When a loader is aborted before producing data, the match was promoted from `pending` to `success` without setting `loaderData`. The route component then rendered while `useLoaderData()` returned `undefined`, violating its non-undefined type. Resolve such a match to `error` instead so the errorComponent renders. Stale-while-revalidate (aborts on an already-loaded match) and the rapid-navigation abort path are preserved.
1 parent 279a849 commit 8ec67d4

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-core': patch
3+
---
4+
5+
Resolve a match to `error` instead of `success` when its loader is aborted before producing data. Previously an aborted loader could leave the match in `success` with `loaderData` undefined, so the route component rendered while `useLoaderData()` returned `undefined` despite its non-undefined type.

‎packages/react-router/tests/loaders.test.tsx‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,39 @@ test('throw abortError from loader upon initial load with basepath', async () =>
751751

752752
render(<RouterProvider router={router} />)
753753

754-
const indexElement = await screen.findByText('Index route content')
755-
expect(indexElement).toBeInTheDocument()
756-
expect(screen.queryByTestId('index-error')).not.toBeInTheDocument()
754+
const errorElement = await screen.findByTestId('index-error')
755+
expect(errorElement).toBeInTheDocument()
756+
expect(screen.queryByText('Index route content')).not.toBeInTheDocument()
757757
expect(window.location.pathname.startsWith('/app')).toBe(true)
758758
})
759759

760+
test('aborted loader does not render the route component with undefined loaderData', async () => {
761+
const rootRoute = createRootRoute({})
762+
763+
const indexRoute = createRoute({
764+
getParentRoute: () => rootRoute,
765+
path: '/',
766+
loader: async (): Promise<{ value: string }> => {
767+
return Promise.reject(new DOMException('Aborted', 'AbortError'))
768+
},
769+
component: () => {
770+
const data = indexRoute.useLoaderData()
771+
return <div data-testid="index-content">value: {data.value}</div>
772+
},
773+
errorComponent: () => (
774+
<div data-testid="index-error">indexErrorComponent</div>
775+
),
776+
})
777+
778+
const routeTree = rootRoute.addChildren([indexRoute])
779+
const router = createRouter({ routeTree, history })
780+
781+
render(<RouterProvider router={router} />)
782+
783+
expect(await screen.findByTestId('index-error')).toBeInTheDocument()
784+
expect(screen.queryByTestId('index-content')).not.toBeInTheDocument()
785+
})
786+
760787
test('cancelMatches after pending timeout', async () => {
761788
function getPendingComponent(onMount: () => void) {
762789
const PendingComponent = () => {

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,19 @@ const runLoader = async (
731731
match._nonReactive.loaderPromise = undefined
732732
return
733733
}
734-
inner.updateMatch(matchId, (prev) => ({
735-
...prev,
736-
status: prev.status === 'pending' ? 'success' : prev.status,
737-
isFetching: false,
738-
context: buildMatchContext(inner, index),
739-
}))
740-
return
734+
// a previous load already produced data, so keep showing it instead of
735+
// surfacing the abort
736+
if (inner.router.getMatch(matchId)?.status !== 'pending') {
737+
inner.updateMatch(matchId, (prev) => ({
738+
...prev,
739+
isFetching: false,
740+
context: buildMatchContext(inner, index),
741+
}))
742+
return
743+
}
744+
// no loaderData yet: fall through to error handling rather than
745+
// resolving to `success`, which would render the route component with
746+
// loaderData undefined
741747
}
742748

743749
const pendingPromise = match._nonReactive.minPendingPromise

‎packages/solid-router/tests/loaders.test.tsx‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,39 @@ test('throw abortError from loader upon initial load with basepath', async () =>
342342

343343
render(() => <RouterProvider router={router} />)
344344

345-
const indexElement = await screen.findByText('Index route content')
346-
expect(indexElement).toBeInTheDocument()
347-
expect(screen.queryByTestId('index-error')).not.toBeInTheDocument()
345+
const errorElement = await screen.findByTestId('index-error')
346+
expect(errorElement).toBeInTheDocument()
347+
expect(screen.queryByText('Index route content')).not.toBeInTheDocument()
348348
expect(window.location.pathname.startsWith('/app')).toBe(true)
349349
})
350350

351+
test('aborted loader does not render the route component with undefined loaderData', async () => {
352+
const rootRoute = createRootRoute({})
353+
354+
const indexRoute = createRoute({
355+
getParentRoute: () => rootRoute,
356+
path: '/',
357+
loader: async (): Promise<{ value: string }> => {
358+
return Promise.reject(new DOMException('Aborted', 'AbortError'))
359+
},
360+
component: () => {
361+
const data = indexRoute.useLoaderData()
362+
return <div data-testid="index-content">value: {data().value}</div>
363+
},
364+
errorComponent: () => (
365+
<div data-testid="index-error">indexErrorComponent</div>
366+
),
367+
})
368+
369+
const routeTree = rootRoute.addChildren([indexRoute])
370+
const router = createRouter({ routeTree })
371+
372+
render(() => <RouterProvider router={router} />)
373+
374+
expect(await screen.findByTestId('index-error')).toBeInTheDocument()
375+
expect(screen.queryByTestId('index-content')).not.toBeInTheDocument()
376+
})
377+
351378
test('reproducer #4245', async () => {
352379
const LOADER_WAIT_TIME = 500
353380
const rootRoute = createRootRoute({})

0 commit comments

Comments
 (0)