Skip to content

Commit 0bf8243

Browse files
single function for match manipulation
1 parent 07cde45 commit 0bf8243

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

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

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,23 @@ const buildMatchContext = (
6868
return context
6969
}
7070

71-
// Commits the merged context exactly when a match's beforeLoad phase settles.
72-
// Loader-phase updates intentionally leave context alone; loaders cannot change
73-
// the inputs used by buildMatchContext.
74-
const commitMatch = (
75-
inner: InnerLoadContext,
76-
matchId: string,
77-
index: number,
78-
patch: Partial<AnyRouteMatch>,
79-
): void => {
80-
inner.updateMatch(matchId, (prev) => ({
81-
...prev,
82-
...patch,
83-
context: buildMatchContext(inner, index),
84-
}))
85-
}
86-
8771
const patchMatch = (
8872
inner: InnerLoadContext,
8973
matchId: string,
9074
patch: Partial<AnyRouteMatch>,
75+
contextIndex?: number,
9176
): void => {
92-
inner.updateMatch(matchId, (prev) => ({
93-
...prev,
94-
...patch,
95-
}))
77+
inner.updateMatch(matchId, (prev) => {
78+
const next = {
79+
...prev,
80+
...patch,
81+
}
82+
if (contextIndex !== undefined) {
83+
// Context is only recomputed when the beforeLoad/SSR-skip phase settles.
84+
next.context = buildMatchContext(inner, contextIndex)
85+
}
86+
return next
87+
})
9688
}
9789

9890
const getNavigate = (inner: InnerLoadContext) => (opts: any) =>
@@ -300,15 +292,20 @@ const handleSerialError = (
300292

301293
// A match that errors during the beforeLoad phase never reaches the loader
302294
// phase. Settle its promises after committing the error state.
303-
commitMatch(inner, matchId, index, {
304-
__beforeLoadContext: undefined,
305-
error: err,
306-
status: 'error',
307-
isFetching: false,
308-
_forcePending: undefined,
309-
updatedAt: Date.now(),
310-
abortController: new AbortController(),
311-
})
295+
patchMatch(
296+
inner,
297+
matchId,
298+
{
299+
__beforeLoadContext: undefined,
300+
error: err,
301+
status: 'error',
302+
isFetching: false,
303+
_forcePending: undefined,
304+
updatedAt: Date.now(),
305+
abortController: new AbortController(),
306+
},
307+
index,
308+
)
312309

313310
const updatedMatch = inner.router.getMatch(matchId)
314311
if (updatedMatch) {
@@ -482,10 +479,15 @@ const executeBeforeLoad = (
482479
inner.matches[index]!.__beforeLoadContext = undefined
483480
inner.router.batch(() => {
484481
pending()
485-
commitMatch(inner, matchId, index, {
486-
isFetching: false as const,
487-
__beforeLoadContext: undefined,
488-
})
482+
patchMatch(
483+
inner,
484+
matchId,
485+
{
486+
isFetching: false as const,
487+
__beforeLoadContext: undefined,
488+
},
489+
index,
490+
)
489491
})
490492
settleBeforeLoadPromise(match)
491493
return
@@ -512,10 +514,15 @@ const executeBeforeLoad = (
512514

513515
inner.router.batch(() => {
514516
pending()
515-
commitMatch(inner, matchId, index, {
516-
isFetching: false as const,
517-
__beforeLoadContext: beforeLoadContext,
518-
})
517+
patchMatch(
518+
inner,
519+
matchId,
520+
{
521+
isFetching: false as const,
522+
__beforeLoadContext: beforeLoadContext,
523+
},
524+
index,
525+
)
519526
})
520527
settleBeforeLoadPromise(match)
521528
}
@@ -929,7 +936,7 @@ const loadRouteMatch = async (
929936

930937
// the beforeLoad phase (and with it the context commit) does not run for
931938
// skipped matches, so commit the merged route context here
932-
commitMatch(inner, matchId, index, { invalid: false })
939+
patchMatch(inner, matchId, { invalid: false }, index)
933940

934941
if (isServer ?? inner.router.isServer) {
935942
return inner.router.getMatch(matchId)!
@@ -1167,10 +1174,9 @@ export async function loadMatches(arg: {
11671174

11681175
notFoundToThrow.routeId = boundaryMatch.routeId
11691176

1170-
commitMatch(
1177+
patchMatch(
11711178
inner,
11721179
boundaryMatch.id,
1173-
renderedBoundaryIndex,
11741180
boundaryMatch.routeId === rootRouteId
11751181
? // For root boundary, use globalNotFound so the root component's
11761182
// shell still renders and <Outlet> handles the not-found display,
@@ -1190,6 +1196,7 @@ export async function loadMatches(arg: {
11901196
isFetching: false,
11911197
_forcePending: undefined,
11921198
},
1199+
renderedBoundaryIndex,
11931200
)
11941201

11951202
headMaxIndex = renderedBoundaryIndex

0 commit comments

Comments
 (0)