@@ -2370,39 +2370,6 @@ export class RouterCore<
23702370 route : AnyRoute ,
23712371 ) : void | Promise < void > => {
23722372 const match = this . getMatch ( matchId ) !
2373- 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- }
2384-
2385- let isPending = false
2386-
2387- const pending = ( ) => {
2388- isPending = true
2389- innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
2390- ...prev ,
2391- isFetching : 'beforeLoad' ,
2392- fetchCount : prev . fetchCount + 1 ,
2393- abortController,
2394- context,
2395- } ) )
2396- }
2397-
2398- const resolve = ( ) => {
2399- match . _nonReactive . beforeLoadPromise ?. resolve ( )
2400- match . _nonReactive . beforeLoadPromise = undefined
2401- innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
2402- ...prev ,
2403- isFetching : false ,
2404- } ) )
2405- }
24062373
24072374 match . _nonReactive . beforeLoadPromise = createControlledPromise < void > ( )
24082375 // explicitly capture the previous loadPromise
@@ -2411,7 +2378,7 @@ export class RouterCore<
24112378 prevLoadPromise ?. resolve ( )
24122379 } )
24132380
2414- const { paramsError, searchError } = this . getMatch ( matchId ) !
2381+ const { paramsError, searchError } = match
24152382
24162383 if ( paramsError ) {
24172384 this . handleSerialError (
@@ -2433,6 +2400,39 @@ export class RouterCore<
24332400
24342401 this . setupPendingTimeout ( innerLoadContext , matchId , route )
24352402
2403+ const abortController = new AbortController ( )
2404+
2405+ const parentMatchId = innerLoadContext . matches [ index - 1 ] ?. id
2406+ const parentMatch = parentMatchId
2407+ ? this . getMatch ( parentMatchId ) !
2408+ : undefined
2409+ const parentMatchContext =
2410+ parentMatch ?. context ?? this . options . context ?? undefined
2411+
2412+ const context = { ...parentMatchContext , ...match . __routeContext }
2413+
2414+ let isPending = false
2415+ const pending = ( ) => {
2416+ if ( isPending ) return
2417+ isPending = true
2418+ innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
2419+ ...prev ,
2420+ isFetching : 'beforeLoad' ,
2421+ fetchCount : prev . fetchCount + 1 ,
2422+ abortController,
2423+ context,
2424+ } ) )
2425+ }
2426+
2427+ const resolve = ( ) => {
2428+ match . _nonReactive . beforeLoadPromise ?. resolve ( )
2429+ match . _nonReactive . beforeLoadPromise = undefined
2430+ innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
2431+ ...prev ,
2432+ isFetching : false ,
2433+ } ) )
2434+ }
2435+
24362436 // if there is no `beforeLoad` option, skip everything, batch update the store, return early
24372437 if ( ! route . options . beforeLoad ) {
24382438 batch ( ( ) => {
@@ -2442,24 +2442,48 @@ export class RouterCore<
24422442 return
24432443 }
24442444
2445+ const { search, params, cause } = match
2446+ const preload = this . resolvePreload ( innerLoadContext , matchId )
2447+ const beforeLoadFnContext : BeforeLoadContextOptions <
2448+ any ,
2449+ any ,
2450+ any ,
2451+ any ,
2452+ any
2453+ > = {
2454+ search,
2455+ abortController,
2456+ params,
2457+ preload,
2458+ context,
2459+ location : innerLoadContext . location ,
2460+ navigate : ( opts : any ) =>
2461+ this . navigate ( { ...opts , _fromLocation : innerLoadContext . location } ) ,
2462+ buildLocation : this . buildLocation ,
2463+ cause : preload ? 'preload' : cause ,
2464+ matches : innerLoadContext . matches ,
2465+ }
2466+
24452467 const updateContext = ( beforeLoadContext : any ) => {
24462468 if ( beforeLoadContext === undefined ) {
24472469 batch ( ( ) => {
2448- if ( ! isPending ) pending ( )
2470+ pending ( )
24492471 resolve ( )
24502472 } )
24512473 return
24522474 }
24532475 if ( isRedirect ( beforeLoadContext ) || isNotFound ( beforeLoadContext ) ) {
2476+ pending ( )
24542477 this . handleSerialError (
24552478 innerLoadContext ,
24562479 index ,
24572480 beforeLoadContext ,
24582481 'BEFORE_LOAD' ,
24592482 )
24602483 }
2484+
24612485 batch ( ( ) => {
2462- if ( ! isPending ) pending ( )
2486+ pending ( )
24632487 innerLoadContext . updateMatch ( matchId , ( prev ) => ( {
24642488 ...prev ,
24652489 __beforeLoadContext : beforeLoadContext ,
@@ -2471,43 +2495,24 @@ export class RouterCore<
24712495 resolve ( )
24722496 } )
24732497 }
2474- const { search, params, cause } = match
2475- const preload = this . resolvePreload ( innerLoadContext , matchId )
2476- const beforeLoadFnContext : BeforeLoadContextOptions <
2477- any ,
2478- any ,
2479- any ,
2480- any ,
2481- any
2482- > = {
2483- search,
2484- abortController,
2485- params,
2486- preload,
2487- context,
2488- location : innerLoadContext . location ,
2489- navigate : ( opts : any ) =>
2490- this . navigate ( {
2491- ...opts ,
2492- _fromLocation : innerLoadContext . location ,
2493- } ) ,
2494- buildLocation : this . buildLocation ,
2495- cause : preload ? 'preload' : cause ,
2496- matches : innerLoadContext . matches ,
2497- }
2498+
2499+ let beforeLoadContext
24982500 try {
2499- const beforeLoadContext = route . options . beforeLoad ( beforeLoadFnContext )
2501+ beforeLoadContext = route . options . beforeLoad ( beforeLoadFnContext )
25002502 if ( isPromise ( beforeLoadContext ) ) {
25012503 pending ( )
2502- return beforeLoadContext . then ( updateContext ) . catch ( ( err ) => {
2503- this . handleSerialError ( innerLoadContext , index , err , 'BEFORE_LOAD' )
2504- } )
2505- } else {
2506- updateContext ( beforeLoadContext )
2504+ return beforeLoadContext
2505+ . catch ( ( err ) => {
2506+ this . handleSerialError ( innerLoadContext , index , err , 'BEFORE_LOAD' )
2507+ } )
2508+ . then ( updateContext )
25072509 }
25082510 } catch ( err ) {
2511+ pending ( )
25092512 this . handleSerialError ( innerLoadContext , index , err , 'BEFORE_LOAD' )
25102513 }
2514+
2515+ updateContext ( beforeLoadContext )
25112516 return
25122517 }
25132518
@@ -2927,6 +2932,11 @@ export class RouterCore<
29272932 for ( let i = 0 ; i < innerLoadContext . matches . length ; i ++ ) {
29282933 const beforeLoad = this . handleBeforeLoad ( innerLoadContext , i )
29292934 if ( isPromise ( beforeLoad ) ) await beforeLoad
2935+ console . log (
2936+ 'before load done' ,
2937+ innerLoadContext . matches [ i ] ! . id ,
2938+ this . getMatch ( innerLoadContext . matches [ i ] ! . id ) ?. context ,
2939+ )
29302940 }
29312941
29322942 // Execute all loaders in parallel
0 commit comments