@@ -50,6 +50,29 @@ const resolvePreload = (inner: InnerLoadContext, matchId: string): boolean => {
5050 )
5151}
5252
53+ /**
54+ * Builds the accumulated context from router options and all matches up to (and optionally including) the given index.
55+ * Merges __routeContext and __beforeLoadContext from each match.
56+ */
57+ const buildMatchContext = (
58+ inner : InnerLoadContext ,
59+ index : number ,
60+ includeCurrentMatch : boolean = true ,
61+ ) : Record < string , unknown > => {
62+ const context : Record < string , unknown > = {
63+ ...( inner . router . options . context ?? { } ) ,
64+ }
65+ const end = includeCurrentMatch ? index : index - 1
66+ for ( let i = 0 ; i <= end ; i ++ ) {
67+ const innerMatch = inner . matches [ i ]
68+ if ( ! innerMatch ) continue
69+ const m = inner . router . getMatch ( innerMatch . id )
70+ if ( ! m ) continue
71+ Object . assign ( context , m . __routeContext , m . __beforeLoadContext )
72+ }
73+ return context
74+ }
75+
5376const _handleNotFound = ( inner : InnerLoadContext , err : NotFoundError ) => {
5477 // Find the route that should handle the not found error
5578 // First check if a specific route is requested to show the error
@@ -407,6 +430,12 @@ const executeBeforeLoad = (
407430
408431 match . _nonReactive . beforeLoadPromise = createControlledPromise < void > ( )
409432
433+ // Build context from all parent matches, excluding current match's __beforeLoadContext
434+ // (since we're about to execute beforeLoad for this match)
435+ const context = {
436+ ...buildMatchContext ( inner , index , false ) ,
437+ ...match . __routeContext ,
438+ }
410439 const { search, params, cause } = match
411440 const preload = resolvePreload ( inner , matchId )
412441 const beforeLoadFnContext : BeforeLoadContextOptions <
@@ -423,12 +452,7 @@ const executeBeforeLoad = (
423452 abortController,
424453 params,
425454 preload,
426- // Include parent's __beforeLoadContext so child routes can access it during their beforeLoad
427- context : {
428- ...parentMatchContext ,
429- ...parentMatch ?. __beforeLoadContext ,
430- ...match . __routeContext ,
431- } ,
455+ context,
432456 location : inner . location ,
433457 navigate : ( opts : any ) =>
434458 inner . router . navigate ( {
@@ -569,19 +593,7 @@ const getLoaderContext = (
569593 const { params, loaderDeps, abortController, cause } =
570594 inner . router . getMatch ( matchId ) !
571595
572- let context = inner . router . options . context ?? { }
573-
574- for ( let i = 0 ; i <= index ; i ++ ) {
575- const innerMatch = inner . matches [ i ]
576- if ( ! innerMatch ) continue
577- const m = inner . router . getMatch ( innerMatch . id )
578- if ( ! m ) continue
579- context = {
580- ...context ,
581- ...( m . __routeContext ?? { } ) ,
582- ...( m . __beforeLoadContext ?? { } ) ,
583- }
584- }
596+ const context = buildMatchContext ( inner , index )
585597
586598 const preload = resolvePreload ( inner , matchId )
587599
@@ -747,19 +759,9 @@ const loadRouteMatch = async (
747759 const route = inner . router . looseRoutesById [ routeId ] !
748760
749761 const commitContext = ( ) => {
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- }
759-
760762 inner . updateMatch ( matchId , ( prev ) => ( {
761763 ...prev ,
762- context,
764+ context : buildMatchContext ( inner , index ) ,
763765 } ) )
764766 }
765767
0 commit comments