Skip to content

Commit a95edcb

Browse files
committed
fix(router-core): refine context reconstruction
1 parent 1f760bd commit a95edcb

3 files changed

Lines changed: 46 additions & 42 deletions

File tree

‎packages/react-router/tests/issue-8115-context.test.tsx‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -710,33 +710,34 @@ test('a same-id navigation merges new inherited context with cached route contex
710710
context: ({ location }) => ({
711711
selfRevision: location.state.issue8115Revision,
712712
}),
713-
component: () => {
714-
const location = useLocation()
715-
const context = indexRoute.useRouteContext()
716-
const matchId = indexRoute.useMatch({ select: (match) => match.id })
717-
const navigate = indexRoute.useNavigate()
718-
719-
return (
720-
<>
721-
<output data-testid="match-id">{matchId}</output>
722-
<output data-testid="snapshot">
723-
location: {location.state.issue8115Revision}; inherited:{' '}
724-
{context.inheritedRevision}; self: {context.selfRevision}
725-
</output>
726-
<button
727-
onClick={() => {
728-
void navigate({
729-
to: '/',
730-
state: { issue8115Revision: 'new' },
731-
})
732-
}}
733-
>
734-
Update state
735-
</button>
736-
</>
737-
)
738-
},
739-
})
713+
component: IndexComponent,
714+
})
715+
function IndexComponent() {
716+
const location = useLocation()
717+
const context = indexRoute.useRouteContext()
718+
const matchId = indexRoute.useMatch({ select: (match) => match.id })
719+
const navigate = indexRoute.useNavigate()
720+
721+
return (
722+
<>
723+
<output data-testid="match-id">{matchId}</output>
724+
<output data-testid="snapshot">
725+
location: {location.state.issue8115Revision}; inherited:{' '}
726+
{context.inheritedRevision}; self: {context.selfRevision}
727+
</output>
728+
<button
729+
onClick={() => {
730+
void navigate({
731+
to: '/',
732+
state: { issue8115Revision: 'new' },
733+
})
734+
}}
735+
>
736+
Update state
737+
</button>
738+
</>
739+
)
740+
}
740741
const router = createRouter({
741742
routeTree: rootRoute.addChildren([indexRoute]),
742743
history,

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,22 @@ async function contextualize(
377377
matches,
378378
routeId: route.id,
379379
}
380-
let context = parentContext
380+
let context: typeof parentContext
381381
try {
382-
let routeContext = match._ctx
383-
if (!routeContext && route.options.context) {
384-
routeContext = match._ctx =
385-
route.options.context({
386-
...common,
387-
deps: match.loaderDeps,
388-
context: parentContext,
389-
} satisfies RouteContextOptions<any, any, any, any, any>) || {}
390-
}
391-
context = {
382+
// Reuse the route's cached contribution while rebuilding its inheritance.
383+
const routeContext =
384+
match._ctx ||
385+
(route.options.context &&
386+
(match._ctx =
387+
route.options.context({
388+
...common,
389+
deps: match.loaderDeps,
390+
context: parentContext,
391+
} satisfies RouteContextOptions<any, any, any, any, any>) || {}))
392+
match.context = context = {
392393
...parentContext,
393394
...routeContext,
394395
}
395-
match.context = context
396396
} catch (cause) {
397397
match.context = parentContext
398398
releaseFlight(router, match)
@@ -2240,7 +2240,6 @@ export async function hydrate(router: AnyRouter): Promise<void> {
22402240
let pendingBoundary: number | undefined
22412241
let verifiedAssetEnd = 0
22422242
const retryFrom = (index: number) => {
2243-
pendingBoundary = Math.min(pendingBoundary ?? index, index)
22442243
// The failing route's identity is still verified, but no descendant is.
22452244
verifiedAssetEnd = Math.min(verifiedAssetEnd, index + 1)
22462245
const removed = committed.splice(index)
@@ -2420,6 +2419,8 @@ export async function hydrate(router: AnyRouter): Promise<void> {
24202419
match.status !== 'notFound' &&
24212420
!match._notFound
24222421
) {
2422+
// Never present transported success without reconstructed context.
2423+
pendingBoundary = Math.min(pendingBoundary ?? index, index)
24232424
retryFrom(index)
24242425
break
24252426
}

‎packages/router-core/tests/public-hydration-contract.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,8 @@ describe('public hydration contracts', () => {
13211321

13221322
expect(router.state.matches.at(-1)).toMatchObject({
13231323
routeId: pageRoute.id,
1324-
status: 'success',
1324+
status: 'pending',
1325+
ssr: false,
13251326
error: undefined,
13261327
loaderData: 'server data',
13271328
})
@@ -1376,7 +1377,8 @@ describe('public hydration contracts', () => {
13761377
expect(router.state.location.pathname).toBe('/source')
13771378
expect(router.state.matches.at(-1)).toMatchObject({
13781379
routeId: sourceRoute.id,
1379-
status: 'success',
1380+
status: 'pending',
1381+
ssr: false,
13801382
})
13811383
expect(router.state.resolvedLocation).toBeUndefined()
13821384

0 commit comments

Comments
 (0)