@@ -37,40 +37,6 @@ 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-
7440const triggerOnReady = ( inner : InnerLoadContext ) : void | Promise < void > => {
7541 if ( ! inner . rendered ) {
7642 inner . rendered = true
@@ -284,16 +250,23 @@ const handleSerialError = (
284250 }
285251}
286252
253+ const matchIsAvailable = (
254+ match : AnyRouteMatch | undefined ,
255+ ) : match is AnyRouteMatch =>
256+ Boolean ( match && ! match . abortController . signal . aborted )
257+
287258const isBeforeLoadSsr = (
288259 inner : InnerLoadContext ,
289260 matchId : string ,
290261 index : number ,
291262 route : AnyRoute ,
292263) : void | Promise < void > => {
293- const existingMatch = inner . router . getMatch ( matchId ) !
264+ const existingMatch = inner . router . getMatch ( matchId )
265+ if ( ! existingMatch ) return
266+
294267 const parentMatchId = inner . matches [ index - 1 ] ?. id
295268 const parentMatch = parentMatchId
296- ? inner . router . getMatch ( parentMatchId ) !
269+ ? inner . router . getMatch ( parentMatchId )
297270 : undefined
298271
299272 // in SPA mode, only SSR the root route
@@ -393,7 +366,8 @@ const preBeforeLoadSetup = (
393366 matchId : string ,
394367 route : AnyRoute ,
395368) : void | Promise < void > => {
396- const existingMatch = inner . router . getMatch ( matchId ) !
369+ const existingMatch = inner . router . getMatch ( matchId )
370+ if ( ! existingMatch ) return
397371
398372 // If we are in the middle of a load, either of these will be present
399373 // (not to be confused with `loadPromise`, which is always defined)
@@ -406,7 +380,9 @@ const preBeforeLoadSetup = (
406380 setupPendingTimeout ( inner , matchId , route , existingMatch )
407381
408382 const then = ( ) => {
409- const match = inner . router . getMatch ( matchId ) !
383+ const match = inner . router . getMatch ( matchId )
384+ if ( ! match ) return
385+
410386 if (
411387 match . preload &&
412388 ( match . status === 'redirected' || match . status === 'notFound' )
@@ -427,7 +403,8 @@ const executeBeforeLoad = (
427403 index : number ,
428404 route : AnyRoute ,
429405) : void | Promise < void > => {
430- const match = inner . router . getMatch ( matchId ) !
406+ const match = inner . router . getMatch ( matchId )
407+ if ( ! match ) return
431408
432409 // explicitly capture the previous loadPromise
433410 let prevLoadPromise = match . _nonReactive . loadPromise
@@ -643,17 +620,16 @@ const executeHead = (
643620const getLoaderContext = (
644621 inner : InnerLoadContext ,
645622 matchPromises : Array < Promise < AnyRouteMatch > > ,
646- matchId : string ,
623+ match : AnyRouteMatch ,
647624 index : number ,
648625 route : AnyRoute ,
649626) : LoaderFnContext => {
650627 const parentMatchPromise = matchPromises [ index - 1 ] as any
651- const { params, loaderDeps, abortController, cause } =
652- inner . router . getMatch ( matchId ) !
628+ const { params, loaderDeps, abortController, cause } = match
653629
654630 const context = buildMatchContext ( inner , index )
655631
656- const preload = resolvePreload ( inner , matchId )
632+ const preload = resolvePreload ( inner , match . id )
657633
658634 return {
659635 params,
@@ -688,7 +664,8 @@ const runLoader = async (
688664 // before committing to the match and resolving
689665 // the loadPromise
690666
691- const match = inner . router . getMatch ( matchId ) !
667+ const match = inner . router . getMatch ( matchId )
668+ if ( ! matchIsAvailable ( match ) ) return
692669
693670 // Actually run the loader and handle the result
694671 try {
@@ -701,7 +678,7 @@ const runLoader = async (
701678 const loader =
702679 typeof routeLoader === 'function' ? routeLoader : routeLoader ?. handler
703680 const loaderResult = loader ?.(
704- getLoaderContext ( inner , matchPromises , matchId , index , route ) ,
681+ getLoaderContext ( inner , matchPromises , match , index , route ) ,
705682 )
706683 const loaderResultIsPromise = ! ! loader && isPromise ( loaderResult )
707684
@@ -727,6 +704,8 @@ const runLoader = async (
727704 ? await loaderResult
728705 : loaderResult
729706
707+ if ( match . abortController . signal . aborted ) return
708+
730709 handleRedirectAndNotFound (
731710 inner ,
732711 inner . router . getMatch ( matchId ) ,
@@ -750,6 +729,9 @@ const runLoader = async (
750729 // Last but not least, wait for the the components
751730 // to be preloaded before we resolve the match
752731 if ( route . _componentsPromise ) await route . _componentsPromise
732+
733+ if ( match . abortController . signal . aborted ) return
734+
753735 inner . updateMatch ( matchId , ( prev ) => ( {
754736 ...prev ,
755737 error : undefined ,
@@ -761,6 +743,8 @@ const runLoader = async (
761743 } catch ( e ) {
762744 let error = e
763745
746+ if ( match . abortController . signal . aborted ) return
747+
764748 if ( ( error as any ) ?. name === 'AbortError' ) {
765749 if ( match . abortController . signal . aborted ) {
766750 match . _nonReactive . loaderPromise ?. resolve ( )
@@ -822,15 +806,15 @@ const loadRouteMatch = async (
822806 matchPromises : Array < Promise < AnyRouteMatch > > ,
823807 index : number ,
824808) : Promise < AnyRouteMatch > => {
825- let cleanupMatch : AnyRouteMatch | undefined
826-
827809 async function handleLoader (
828810 preload : boolean ,
829811 prevMatch : AnyRouteMatch ,
830812 previousRouteMatchId : string | undefined ,
831813 match : AnyRouteMatch ,
832814 route : AnyRoute ,
833815 ) {
816+ if ( match . abortController . signal . aborted ) return
817+
834818 const age = Date . now ( ) - prevMatch . updatedAt
835819
836820 const staleAge = preload
@@ -847,7 +831,7 @@ const loadRouteMatch = async (
847831 const shouldReload =
848832 typeof shouldReloadOption === 'function'
849833 ? shouldReloadOption (
850- getLoaderContext ( inner , matchPromises , matchId , index , route ) ,
834+ getLoaderContext ( inner , matchPromises , match , index , route ) ,
851835 )
852836 : shouldReloadOption
853837
@@ -904,17 +888,17 @@ const loadRouteMatch = async (
904888 inner . router . options . defaultStaleReloadMode ) !== 'blocking'
905889
906890 if ( shouldSkipLoader ( inner , matchId ) ) {
907- const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
908-
909- cleanupMatch = match
891+ const match = inner . router . getMatch ( matchId )
892+ if ( ! matchIsAvailable ( match ) ) return inner . matches [ index ] !
910893
911894 syncMatchContext ( inner , matchId , index )
912895
913896 if ( isServer ?? inner . router . isServer ) {
914897 return inner . router . getMatch ( matchId ) !
915898 }
916899 } else {
917- const prevMatch = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
900+ const prevMatch = inner . router . getMatch ( matchId )
901+ if ( ! matchIsAvailable ( prevMatch ) ) return inner . matches [ index ] !
918902
919903 // This is where all of the stale-while-revalidate magic happens
920904 const activeIdAtIndex = inner . router . stores . matchesId . state [ index ]
@@ -944,8 +928,10 @@ const loadRouteMatch = async (
944928 return prevMatch
945929 }
946930 await prevMatch . _nonReactive . loaderPromise
947- const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
948- cleanupMatch = match
931+ if ( prevMatch . abortController . signal . aborted ) return inner . matches [ index ] !
932+
933+ const match = inner . router . getMatch ( matchId )
934+ if ( ! matchIsAvailable ( match ) ) return inner . matches [ index ] !
949935
950936 const error = match . _nonReactive . error || match . error
951937 if ( error ) {
@@ -964,8 +950,8 @@ const loadRouteMatch = async (
964950 } else {
965951 const nextPreload =
966952 preload && ! inner . router . stores . activeMatchStoresById . has ( matchId )
967- const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
968- cleanupMatch = match
953+ const match = inner . router . getMatch ( matchId )
954+ if ( ! matchIsAvailable ( match ) ) return inner . matches [ index ] !
969955
970956 match . _nonReactive . loaderPromise = createControlledPromise < void > ( )
971957 if ( nextPreload !== match . preload ) {
@@ -978,7 +964,9 @@ const loadRouteMatch = async (
978964 await handleLoader ( preload , prevMatch , previousRouteMatchId , match , route )
979965 }
980966 }
981- const match = getMatchOrThrowCancelled ( inner , matchId , cleanupMatch )
967+ const match = inner . router . getMatch ( matchId )
968+ if ( ! matchIsAvailable ( match ) ) return inner . matches [ index ] !
969+
982970 if ( ! loaderIsRunningAsync ) {
983971 match . _nonReactive . loaderPromise ?. resolve ( )
984972 match . _nonReactive . loadPromise ?. resolve ( )
@@ -1064,7 +1052,6 @@ export async function loadMatches(arg: {
10641052
10651053 let firstNotFound : NotFoundError | undefined
10661054 let firstUnhandledRejection : unknown
1067- let firstCancelledMatch : MatchLoadCancelledError | undefined
10681055
10691056 for ( let i = 0 ; i < maxIndexExclusive ; i ++ ) {
10701057 matchPromises . push ( loadRouteMatch ( inner , matchPromises , i ) )
@@ -1079,10 +1066,6 @@ export async function loadMatches(arg: {
10791066 if ( result . status !== 'rejected' ) continue
10801067
10811068 const reason = result . reason
1082- if ( isMatchLoadCancelledError ( reason ) ) {
1083- firstCancelledMatch ??= reason
1084- continue
1085- }
10861069 if ( isRedirect ( reason ) ) {
10871070 throw reason
10881071 }
@@ -1093,10 +1076,6 @@ export async function loadMatches(arg: {
10931076 }
10941077 }
10951078
1096- if ( firstCancelledMatch ) {
1097- throw firstCancelledMatch
1098- }
1099-
11001079 if ( firstUnhandledRejection !== undefined ) {
11011080 throw firstUnhandledRejection
11021081 }
@@ -1163,6 +1142,10 @@ export async function loadMatches(arg: {
11631142 // lazy notFoundComponent) is loaded before we continue to head execution/render.
11641143 await loadRouteChunk ( boundaryRoute , [ 'notFoundComponent' ] )
11651144 } else if ( ! inner . preload ) {
1145+ if ( inner . router . stores . location . state . href !== inner . location . href ) {
1146+ return inner . matches
1147+ }
1148+
11661149 // Clear stale root global-not-found state on normal navigations that do not
11671150 // throw notFound. This must live here (instead of only in runLoader success)
11681151 // because the root loader may be skipped when data is still fresh.
0 commit comments