Skip to content

Commit 562ad18

Browse files
fix child context aggregation
1 parent 131445c commit 562ad18

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ const executeBeforeLoad = (
371371
const parentMatchContext =
372372
parentMatch?.context ?? inner.router.options.context ?? undefined
373373

374-
375374
let isPending = false
376375
const pending = () => {
377376
if (isPending) return
@@ -425,7 +424,11 @@ const executeBeforeLoad = (
425424
params,
426425
preload,
427426
// Include parent's __beforeLoadContext so child routes can access it during their beforeLoad
428-
context: { ...parentMatchContext, ...parentMatch?.__beforeLoadContext, ...match.__routeContext },
427+
context: {
428+
...parentMatchContext,
429+
...parentMatch?.__beforeLoadContext,
430+
...match.__routeContext,
431+
},
429432
location: inner.location,
430433
navigate: (opts: any) =>
431434
inner.router.navigate({
@@ -743,22 +746,20 @@ const loadRouteMatch = async (
743746
let loaderIsRunningAsync = false
744747
const route = inner.router.looseRoutesById[routeId]!
745748

746-
// Helper to compute and commit context after loader completes
747749
const commitContext = () => {
748-
const parentMatchId = inner.matches[index - 1]?.id
749-
const parentMatch = parentMatchId
750-
? inner.router.getMatch(parentMatchId)!
751-
: undefined
752-
const parentContext =
753-
parentMatch?.context ?? inner.router.options.context ?? undefined
750+
const context = { ...inner.router.options.context }
751+
752+
for (let i = 0; i <= index; i++) {
753+
const innerMatch = inner.matches[i]
754+
if (!innerMatch) continue
755+
const m = inner.router.getMatch(innerMatch.id)
756+
if (!m) continue
757+
Object.assign(context, m.__routeContext, m.__beforeLoadContext)
758+
}
754759

755760
inner.updateMatch(matchId, (prev) => ({
756761
...prev,
757-
context: {
758-
...parentContext,
759-
...prev.__routeContext,
760-
...prev.__beforeLoadContext,
761-
},
762+
context,
762763
}))
763764
}
764765

0 commit comments

Comments
 (0)