@@ -8,6 +8,7 @@ import type { GLOBAL_SEROVAL, GLOBAL_TSR } from './ssr/constants'
88import type { AnySerializationAdapter } from './ssr/serializer/transformer'
99import type { TsrSsrGlobal } from './ssr/types'
1010import type { ParsedLocation } from './location'
11+ import type { NavigateOptions } from './link'
1112import type { AnyRouteMatch } from './Matches'
1213import type { NotFoundError } from './not-found'
1314import type {
@@ -18,7 +19,9 @@ import type {
1819 RouteLoaderFn ,
1920} from './route'
2021import type { AnyRedirect } from './redirect'
21- import type { AnyRouter } from './router'
22+ import type { AnyRouter , RouterCore , TrailingSlashOption } from './router'
23+ import type { RoutePaths } from './routeInfo'
24+ import type { RouterHistory } from '@tanstack/history'
2225
2326type RouteComponentType =
2427 | 'component'
@@ -2071,71 +2074,101 @@ export async function refreshClientRoute(
20712074 await loadClientRoute ( router , { sync : true } )
20722075}
20732076
2074- export async function preloadClientRoute (
2075- router : CoordinatorRouter ,
2076- opts : any ,
2077- redirects = 0 ,
2078- builtLocation ?: ParsedLocation ,
2077+ export async function preloadClientRoute <
2078+ TRouteTree extends AnyRoute ,
2079+ TTrailingSlashOption extends TrailingSlashOption ,
2080+ TDefaultStructuralSharingOption extends boolean ,
2081+ TRouterHistory extends RouterHistory ,
2082+ TDehydrated extends Record < string , any > = Record < string , any > ,
2083+ TFrom extends RoutePaths < TRouteTree > | string = string ,
2084+ TTo extends string | undefined = undefined ,
2085+ TMaskFrom extends RoutePaths < TRouteTree > | string = TFrom ,
2086+ TMaskTo extends string = '' ,
2087+ > (
2088+ router : RouterCore <
2089+ TRouteTree ,
2090+ TTrailingSlashOption ,
2091+ TDefaultStructuralSharingOption ,
2092+ TRouterHistory ,
2093+ TDehydrated
2094+ > ,
2095+ opts : NavigateOptions <
2096+ RouterCore <
2097+ TRouteTree ,
2098+ TTrailingSlashOption ,
2099+ TDefaultStructuralSharingOption ,
2100+ TRouterHistory ,
2101+ TDehydrated
2102+ > ,
2103+ TFrom ,
2104+ TTo ,
2105+ TMaskFrom ,
2106+ TMaskTo
2107+ > ,
20792108) : Promise < Array < AnyRouteMatch > | undefined > {
20802109 if (
20812110 process . env . NODE_ENV !== 'production' &&
2082- ( router . _refreshNextLoad || router . _tx ?. [ 6 /* refresh */ ] )
2111+ ( ( router as CoordinatorRouter ) . _refreshNextLoad ||
2112+ router . _tx ?. [ 6 /* refresh */ ] )
20832113 ) {
20842114 return
20852115 }
2086- const location = builtLocation ?? router . buildLocation ( opts )
2087- const base = router . _committed
2088- const controller = new AbortController ( )
2089- let matches : Array < AnyRouteMatch >
2090- try {
2091- matches = router . matchRoutes ( location , {
2092- _controller : controller ,
2093- } )
2094- acquireMatchResources ( matches )
2095- } catch ( cause ) {
2096- controller . abort ( )
2097- if ( ! isNotFound ( cause ) ) {
2098- console . error ( cause )
2099- }
2100- return
2101- }
2102- ; ( router . _preloads ??= new Map ( ) ) . set ( controller , matches )
2103- let active : boolean
2104- try {
2105- let result : LaneResult
2116+ let location = router . buildLocation (
2117+ opts as Parameters < typeof router . buildLocation > [ 0 ] ,
2118+ )
2119+ for ( let redirects = 0 ; ; redirects ++ ) {
2120+ const base = router . _committed
2121+ const controller = new AbortController ( )
2122+ let matches : Array < AnyRouteMatch >
21062123 try {
2107- result = await executeClientLane ( router , location , matches , [
2108- controller ,
2109- redirects ,
2110- base ,
2111- true ,
2112- ] )
2113- } finally {
2114- active = router . _preloads . delete ( controller )
2115- transferMatchResources ( router , matches )
2124+ matches = router . matchRoutes ( location , {
2125+ _controller : controller ,
2126+ } )
2127+ acquireMatchResources ( matches )
2128+ } catch ( cause ) {
21162129 controller . abort ( )
2130+ if ( ! isNotFound ( cause ) ) {
2131+ console . error ( cause )
2132+ }
2133+ return
21172134 }
2118- if ( ! isControl ( result ) ) {
2119- return result [ 1 /* matches */ ]
2120- }
2121- if (
2122- active &&
2123- result [ 0 /* kind */ ] === REDIRECTED &&
2124- ! result [ 1 /* redirect */ ] . options . reloadDocument
2125- ) {
2126- return preloadClientRoute (
2127- router ,
2128- result [ 1 /* redirect */ ] . options ,
2129- redirects + 1 ,
2130- result [ 2 /* location */ ] ,
2131- )
2132- }
2133- } catch ( cause ) {
2134- if ( ! isNotFound ( cause ) ) {
2135- console . error ( cause )
2135+ ; ( router . _preloads ??= new Map ( ) ) . set ( controller , matches )
2136+ let active : boolean
2137+ try {
2138+ let result : LaneResult
2139+ try {
2140+ result = await executeClientLane ( router , location , matches , [
2141+ controller ,
2142+ redirects ,
2143+ base ,
2144+ true ,
2145+ ] )
2146+ } finally {
2147+ active = router . _preloads . delete ( controller )
2148+ transferMatchResources ( router , matches )
2149+ controller . abort ( )
2150+ }
2151+ if ( ! isControl ( result ) ) {
2152+ return result [ 1 /* matches */ ]
2153+ }
2154+ // Only a materialized redirect has a third tuple item.
2155+ if (
2156+ ! active ||
2157+ result . length < 3 ||
2158+ ( process . env . NODE_ENV !== 'production' &&
2159+ ( ( router as CoordinatorRouter ) . _refreshNextLoad ||
2160+ router . _tx ?. [ 6 /* refresh */ ] ) )
2161+ ) {
2162+ return
2163+ }
2164+ location = result [ 2 /* location */ ] !
2165+ } catch ( cause ) {
2166+ if ( ! isNotFound ( cause ) ) {
2167+ console . error ( cause )
2168+ }
2169+ return
21362170 }
21372171 }
2138- return
21392172}
21402173
21412174// --- SSR hydration (client entry via @tanstack/router-core/ssr/client) ---
0 commit comments