@@ -2371,24 +2371,27 @@ export class RouterCore<
23712371 ) : void | Promise < void > => {
23722372 const match = this . getMatch ( matchId ) !
23732373 const abortController = new AbortController ( )
2374+ const parentMatchId = innerLoadContext . matches [ index - 1 ] ?. id
2375+ const parentMatch = parentMatchId
2376+ ? this . getMatch ( parentMatchId ) !
2377+ : undefined
2378+ const parentMatchContext =
2379+ parentMatch ?. context ?? this . options . context ?? undefined
2380+ const context = {
2381+ ...parentMatchContext ,
2382+ ...match . __routeContext ,
2383+ }
23742384
2375- const pending = ( ) => {
2376- const parentMatchId = innerLoadContext . matches [ index - 1 ] ?. id
2377- const parentMatch = parentMatchId
2378- ? this . getMatch ( parentMatchId ) !
2379- : undefined
2380- const parentMatchContext =
2381- parentMatch ?. context ?? this . options . context ?? undefined
2385+ let isPending = false
23822386
2387+ const pending = ( ) => {
2388+ isPending = true
23832389 innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
23842390 ...prev ,
23852391 isFetching : 'beforeLoad' ,
23862392 fetchCount : prev . fetchCount + 1 ,
23872393 abortController,
2388- context : {
2389- ...parentMatchContext ,
2390- ...prev . __routeContext ,
2391- } ,
2394+ context,
23922395 } ) )
23932396 }
23942397
@@ -2439,11 +2442,12 @@ export class RouterCore<
24392442 return
24402443 }
24412444
2442- pending ( )
2443-
24442445 const updateContext = ( beforeLoadContext : any ) => {
24452446 if ( beforeLoadContext === undefined ) {
2446- resolve ( )
2447+ batch ( ( ) => {
2448+ if ( ! isPending ) pending ( )
2449+ resolve ( )
2450+ } )
24472451 return
24482452 }
24492453 if ( isRedirect ( beforeLoadContext ) || isNotFound ( beforeLoadContext ) ) {
@@ -2455,6 +2459,7 @@ export class RouterCore<
24552459 )
24562460 }
24572461 batch ( ( ) => {
2462+ if ( ! isPending ) pending ( )
24582463 innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
24592464 ...prev ,
24602465 __beforeLoadContext : beforeLoadContext ,
@@ -2466,7 +2471,7 @@ export class RouterCore<
24662471 resolve ( )
24672472 } )
24682473 }
2469- const { search, params, context , cause } = this . getMatch ( matchId ) !
2474+ const { search, params, cause } = match
24702475 const preload = this . resolvePreload ( innerLoadContext , matchId )
24712476 const beforeLoadFnContext : BeforeLoadContextOptions <
24722477 any ,
@@ -2493,6 +2498,7 @@ export class RouterCore<
24932498 try {
24942499 const beforeLoadContext = route . options . beforeLoad ( beforeLoadFnContext )
24952500 if ( isPromise ( beforeLoadContext ) ) {
2501+ pending ( )
24962502 return beforeLoadContext . then ( updateContext ) . catch ( ( err ) => {
24972503 this . handleSerialError ( innerLoadContext , index , err , 'BEFORE_LOAD' )
24982504 } )
0 commit comments