@@ -37,6 +37,40 @@ type InnerLoadContext = {
3737 sync ?: boolean
3838}
3939
40+ const reason = 'Match removed before complete load'
41+ class MatchLoadCancelledError extends Error {
42+ constructor ( ) {
43+ super ( reason )
44+ this . name = 'MatchCancel'
45+ }
46+ }
47+
48+ export const isMatchLoadCancelledError = (
49+ err : unknown ,
50+ ) : err is MatchLoadCancelledError => err instanceof MatchLoadCancelledError
51+
52+ const getMatchOrThrowCancelled = (
53+ inner : InnerLoadContext ,
54+ matchId : string ,
55+ cleanupMatch ?: AnyRouteMatch ,
56+ ) : AnyRouteMatch => {
57+ const match = inner . router . getMatch ( matchId )
58+ if ( match ) return match as AnyRouteMatch
59+ if ( cleanupMatch ) {
60+ const s = cleanupMatch . _nonReactive
61+ s . beforeLoadPromise ?. resolve ( )
62+ s . loaderPromise ?. resolve ( )
63+ s . loadPromise ?. resolve ( )
64+ cleanupMatch . abortController . abort ( reason )
65+ clearTimeout ( s . pendingTimeout )
66+ s . beforeLoadPromise = undefined
67+ s . loaderPromise = undefined
68+ s . loadPromise = undefined
69+ s . pendingTimeout = undefined
70+ }
71+ throw new MatchLoadCancelledError ( )
72+ }
73+
4074const triggerOnReady = ( inner : InnerLoadContext ) : void | Promise < void > => {
4175 if ( ! inner . rendered ) {
4276 inner . rendered = true
@@ -788,6 +822,8 @@ const loadRouteMatch = async (
788822 matchPromises : Array < Promise < AnyRouteMatch > > ,
789823 index : number ,
790824) : Promise < AnyRouteMatch > => {
825+ let cleanupMatch : AnyRouteMatch | undefined
826+
791827 async function handleLoader (
792828 preload : boolean ,
793829 prevMatch : AnyRouteMatch ,
@@ -868,21 +904,17 @@ const loadRouteMatch = async (
868904 inner . router . options . defaultStaleReloadMode ) !== 'blocking'
869905
870906 if ( shouldSkipLoader ( inner , matchId ) ) {
871- const match = inner . router . getMatch ( matchId )
872- if ( ! match ) {
873- return inner . matches [ index ] !
874- }
907+ const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
908+
909+ cleanupMatch = match
875910
876911 syncMatchContext ( inner , matchId , index )
877912
878913 if ( isServer ?? inner . router . isServer ) {
879914 return inner . router . getMatch ( matchId ) !
880915 }
881916 } else {
882- const prevMatch = inner . router . getMatch ( matchId )
883- if ( ! prevMatch ) {
884- return inner . matches [ index ] !
885- }
917+ const prevMatch = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
886918
887919 // This is where all of the stale-while-revalidate magic happens
888920 const activeIdAtIndex = inner . router . stores . matchesId . state [ index ]
@@ -912,10 +944,8 @@ const loadRouteMatch = async (
912944 return prevMatch
913945 }
914946 await prevMatch . _nonReactive . loaderPromise
915- const match = inner . router . getMatch ( matchId )
916- if ( ! match ) {
917- return inner . matches [ index ] !
918- }
947+ const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
948+ cleanupMatch = match
919949
920950 const error = match . _nonReactive . error || match . error
921951 if ( error ) {
@@ -934,10 +964,8 @@ const loadRouteMatch = async (
934964 } else {
935965 const nextPreload =
936966 preload && ! inner . router . stores . activeMatchStoresById . has ( matchId )
937- const match = inner . router . getMatch ( matchId )
938- if ( ! match ) {
939- return inner . matches [ index ] !
940- }
967+ const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
968+ cleanupMatch = match
941969
942970 match . _nonReactive . loaderPromise = createControlledPromise < void > ( )
943971 if ( nextPreload !== match . preload ) {
@@ -950,11 +978,7 @@ const loadRouteMatch = async (
950978 await handleLoader ( preload , prevMatch , previousRouteMatchId , match , route )
951979 }
952980 }
953- const match = inner . router . getMatch ( matchId )
954- if ( ! match ) {
955- return inner . matches [ index ] !
956- }
957-
981+ const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
958982 if ( ! loaderIsRunningAsync ) {
959983 match . _nonReactive . loaderPromise ?. resolve ( )
960984 match . _nonReactive . loadPromise ?. resolve ( )
@@ -973,10 +997,8 @@ const loadRouteMatch = async (
973997 isFetching : nextIsFetching ,
974998 invalid : false ,
975999 } ) )
976- return inner . router . getMatch ( matchId ) ?? match
977- } else {
978- return match
9791000 }
1001+ return match
9801002}
9811003
9821004export async function loadMatches ( arg : {
@@ -1042,6 +1064,7 @@ export async function loadMatches(arg: {
10421064
10431065 let firstNotFound : NotFoundError | undefined
10441066 let firstUnhandledRejection : unknown
1067+ let firstCancelledMatch : MatchLoadCancelledError | undefined
10451068
10461069 for ( let i = 0 ; i < maxIndexExclusive ; i ++ ) {
10471070 matchPromises . push ( loadRouteMatch ( inner , matchPromises , i ) )
@@ -1056,6 +1079,10 @@ export async function loadMatches(arg: {
10561079 if ( result . status !== 'rejected' ) continue
10571080
10581081 const reason = result . reason
1082+ if ( isMatchLoadCancelledError ( reason ) ) {
1083+ firstCancelledMatch ??= reason
1084+ continue
1085+ }
10591086 if ( isRedirect ( reason ) ) {
10601087 throw reason
10611088 }
@@ -1066,6 +1093,10 @@ export async function loadMatches(arg: {
10661093 }
10671094 }
10681095
1096+ if ( firstCancelledMatch ) {
1097+ throw firstCancelledMatch
1098+ }
1099+
10691100 if ( firstUnhandledRejection !== undefined ) {
10701101 throw firstUnhandledRejection
10711102 }
0 commit comments