@@ -709,9 +709,10 @@ const loadRouteMatch = (
709
709
index : number ,
710
710
) : Awaitable < AnyRouteMatch > => {
711
711
const { id : matchId , routeId } = inner . matches [ index ] !
712
- let loaderShouldRunAsync = false
713
- let loaderIsRunningAsync = false
714
712
const route = inner . router . looseRoutesById [ routeId ] !
713
+ const prevMatch = inner . router . getMatch ( matchId ) !
714
+ let loaderIsRunningAsync = false
715
+ let nextPreload : undefined | boolean
715
716
716
717
if ( shouldSkipLoader ( inner , matchId ) ) {
717
718
if ( inner . router . isServer ) {
@@ -725,13 +726,11 @@ const loadRouteMatch = (
725
726
return inner . router . getMatch ( matchId ) !
726
727
} )
727
728
}
728
- return inner . router . getMatch ( matchId ) !
729
+ return prevMatch
729
730
}
730
731
return settleLoadRouteMatch ( )
731
732
}
732
733
733
- const prevMatch = inner . router . getMatch ( matchId ) !
734
-
735
734
// there is a loaderPromise, so we are in the middle of a load
736
735
if ( prevMatch . _nonReactive . loaderPromise ) {
737
736
// do not block if we already have stale data we can show
@@ -770,25 +769,18 @@ const loadRouteMatch = (
770
769
? shouldReloadOption ( getLoaderContext ( inner , matchId , index , route ) )
771
770
: shouldReloadOption
772
771
773
- const nextPreload =
772
+ nextPreload =
774
773
! ! preload && ! inner . router . state . matches . some ( ( d ) => d . id === matchId )
775
- const match = inner . router . getMatch ( matchId ) !
776
- match . _nonReactive . loaderPromise = createControlledPromise < void > ( )
777
- if ( nextPreload !== match . preload ) {
778
- inner . updateMatch ( matchId , ( prev ) => ( {
779
- ...prev ,
780
- preload : nextPreload ,
781
- } ) )
782
- }
774
+ prevMatch . _nonReactive . loaderPromise = createControlledPromise < void > ( )
783
775
784
776
if ( preload && route . options . preload === false ) {
785
777
// Do nothing
786
778
return settleLoadRouteMatch ( )
787
779
}
788
780
789
781
// If the route is successful and still fresh, just resolve
790
- const { status, invalid } = match
791
- loaderShouldRunAsync =
782
+ const { status, invalid } = prevMatch
783
+ const loaderShouldRunAsync =
792
784
status === 'success' && ( invalid || ( shouldReload ?? age > staleAge ) )
793
785
if ( loaderShouldRunAsync && ! inner . sync ) {
794
786
loaderIsRunningAsync = true
@@ -809,6 +801,7 @@ const loadRouteMatch = (
809
801
}
810
802
811
803
if ( status !== 'success' || ( loaderShouldRunAsync && inner . sync ) ) {
804
+ updatePreload ( )
812
805
return runLoader ( inner , matchId , index , route ) . then ( settleLoadRouteMatch )
813
806
}
814
807
@@ -818,16 +811,32 @@ const loadRouteMatch = (
818
811
const headResult = executeHead ( inner , matchId , route )
819
812
if ( headResult ) {
820
813
return headResult . then ( ( head ) => {
821
- inner . updateMatch ( matchId , ( prev ) => ( {
822
- ...prev ,
823
- ...head ,
824
- } ) )
825
- return settleLoadRouteMatch ( )
814
+ let result : ReturnType < typeof settleLoadRouteMatch >
815
+ batch ( ( ) => {
816
+ inner . updateMatch ( matchId , ( prev ) => ( {
817
+ ...prev ,
818
+ ...head ,
819
+ } ) )
820
+ result = settleLoadRouteMatch ( )
821
+ } )
822
+ return result !
826
823
} )
827
824
}
828
825
829
826
return settleLoadRouteMatch ( )
830
827
828
+ function updatePreload ( ) {
829
+ if ( nextPreload === undefined ) return
830
+ if ( nextPreload !== prevMatch . preload ) {
831
+ const preload = nextPreload
832
+ inner . updateMatch ( matchId , ( prev ) => ( {
833
+ ...prev ,
834
+ preload,
835
+ } ) )
836
+ }
837
+ nextPreload = undefined
838
+ }
839
+
831
840
function settleLoadRouteMatch ( ) {
832
841
const match = inner . router . getMatch ( matchId ) !
833
842
@@ -843,15 +852,19 @@ const loadRouteMatch = (
843
852
844
853
const nextIsFetching = loaderIsRunningAsync ? match . isFetching : false
845
854
if ( nextIsFetching !== match . isFetching || match . invalid !== false ) {
846
- inner . updateMatch ( matchId , ( prev ) => ( {
847
- ...prev ,
848
- isFetching : nextIsFetching ,
849
- invalid : false ,
850
- } ) )
851
- return inner . router . getMatch ( matchId ) !
855
+ batch ( ( ) => {
856
+ updatePreload ( )
857
+ inner . updateMatch ( matchId , ( prev ) => ( {
858
+ ...prev ,
859
+ isFetching : nextIsFetching ,
860
+ invalid : false ,
861
+ } ) )
862
+ } )
863
+ } else {
864
+ updatePreload ( )
852
865
}
853
866
854
- return match
867
+ return inner . router . getMatch ( matchId ) !
855
868
}
856
869
}
857
870
@@ -886,13 +899,10 @@ export async function loadMatches(arg: {
886
899
887
900
// Execute all loaders in parallel
888
901
const max = inner . firstBadMatchIndex ?? inner . matches . length
889
- let hasPromises = false
890
902
for ( let i = 0 ; i < max ; i ++ ) {
891
- const result = loadRouteMatch ( inner , i )
892
- inner . matchPromises . push ( result )
893
- if ( ! hasPromises && isPromise ( result ) ) hasPromises = true
903
+ inner . matchPromises . push ( loadRouteMatch ( inner , i ) )
894
904
}
895
- if ( hasPromises ) await Promise . all ( inner . matchPromises )
905
+ await Promise . all ( inner . matchPromises )
896
906
897
907
const readyPromise = triggerOnReady ( inner )
898
908
if ( isPromise ( readyPromise ) ) await readyPromise
0 commit comments