@@ -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 {
@@ -17,7 +18,9 @@ import type {
1718 RouteLoaderFn ,
1819} from './route'
1920import type { AnyRedirect } from './redirect'
20- import type { AnyRouter } from './router'
21+ import type { AnyRouter , RouterCore , TrailingSlashOption } from './router'
22+ import type { RoutePaths } from './routeInfo'
23+ import type { RouterHistory } from '@tanstack/history'
2124
2225type RouteComponentType =
2326 | 'component'
@@ -2061,71 +2064,101 @@ export async function refreshClientRoute(
20612064 await loadClientRoute ( router , { sync : true } )
20622065}
20632066
2064- export async function preloadClientRoute (
2065- router : CoordinatorRouter ,
2066- opts : any ,
2067- redirects = 0 ,
2068- builtLocation ?: ParsedLocation ,
2067+ export async function preloadClientRoute <
2068+ TRouteTree extends AnyRoute ,
2069+ TTrailingSlashOption extends TrailingSlashOption ,
2070+ TDefaultStructuralSharingOption extends boolean ,
2071+ TRouterHistory extends RouterHistory ,
2072+ TDehydrated extends Record < string , any > = Record < string , any > ,
2073+ TFrom extends RoutePaths < TRouteTree > | string = string ,
2074+ TTo extends string | undefined = undefined ,
2075+ TMaskFrom extends RoutePaths < TRouteTree > | string = TFrom ,
2076+ TMaskTo extends string = '' ,
2077+ > (
2078+ router : RouterCore <
2079+ TRouteTree ,
2080+ TTrailingSlashOption ,
2081+ TDefaultStructuralSharingOption ,
2082+ TRouterHistory ,
2083+ TDehydrated
2084+ > ,
2085+ opts : NavigateOptions <
2086+ RouterCore <
2087+ TRouteTree ,
2088+ TTrailingSlashOption ,
2089+ TDefaultStructuralSharingOption ,
2090+ TRouterHistory ,
2091+ TDehydrated
2092+ > ,
2093+ TFrom ,
2094+ TTo ,
2095+ TMaskFrom ,
2096+ TMaskTo
2097+ > ,
20692098) : Promise < Array < AnyRouteMatch > | undefined > {
20702099 if (
20712100 process . env . NODE_ENV !== 'production' &&
2072- ( router . _refreshNextLoad || router . _tx ?. [ 6 /* refresh */ ] )
2101+ ( ( router as CoordinatorRouter ) . _refreshNextLoad ||
2102+ router . _tx ?. [ 6 /* refresh */ ] )
20732103 ) {
20742104 return
20752105 }
2076- const location = builtLocation ?? router . buildLocation ( opts )
2077- const base = router . _committed
2078- const controller = new AbortController ( )
2079- let matches : Array < AnyRouteMatch >
2080- try {
2081- matches = router . matchRoutes ( location , {
2082- _controller : controller ,
2083- } )
2084- acquireMatchResources ( matches )
2085- } catch ( cause ) {
2086- controller . abort ( )
2087- if ( ! isNotFound ( cause ) ) {
2088- console . error ( cause )
2089- }
2090- return
2091- }
2092- ; ( router . _preloads ??= new Map ( ) ) . set ( controller , matches )
2093- let active : boolean
2094- try {
2095- let result : LaneResult
2106+ let location = router . buildLocation (
2107+ opts as Parameters < typeof router . buildLocation > [ 0 ] ,
2108+ )
2109+ for ( let redirects = 0 ; ; redirects ++ ) {
2110+ const base = router . _committed
2111+ const controller = new AbortController ( )
2112+ let matches : Array < AnyRouteMatch >
20962113 try {
2097- result = await executeClientLane ( router , location , matches , [
2098- controller ,
2099- redirects ,
2100- base ,
2101- true ,
2102- ] )
2103- } finally {
2104- active = router . _preloads . delete ( controller )
2105- transferMatchResources ( router , matches )
2114+ matches = router . matchRoutes ( location , {
2115+ _controller : controller ,
2116+ } )
2117+ acquireMatchResources ( matches )
2118+ } catch ( cause ) {
21062119 controller . abort ( )
2120+ if ( ! isNotFound ( cause ) ) {
2121+ console . error ( cause )
2122+ }
2123+ return
21072124 }
2108- if ( ! isControl ( result ) ) {
2109- return result [ 1 /* matches */ ]
2110- }
2111- if (
2112- active &&
2113- result [ 0 /* kind */ ] === REDIRECTED &&
2114- ! result [ 1 /* redirect */ ] . options . reloadDocument
2115- ) {
2116- return preloadClientRoute (
2117- router ,
2118- result [ 1 /* redirect */ ] . options ,
2119- redirects + 1 ,
2120- result [ 2 /* location */ ] ,
2121- )
2122- }
2123- } catch ( cause ) {
2124- if ( ! isNotFound ( cause ) ) {
2125- console . error ( cause )
2125+ ; ( router . _preloads ??= new Map ( ) ) . set ( controller , matches )
2126+ let active : boolean
2127+ try {
2128+ let result : LaneResult
2129+ try {
2130+ result = await executeClientLane ( router , location , matches , [
2131+ controller ,
2132+ redirects ,
2133+ base ,
2134+ true ,
2135+ ] )
2136+ } finally {
2137+ active = router . _preloads . delete ( controller )
2138+ transferMatchResources ( router , matches )
2139+ controller . abort ( )
2140+ }
2141+ if ( ! isControl ( result ) ) {
2142+ return result [ 1 /* matches */ ]
2143+ }
2144+ // Only a materialized redirect has a third tuple item.
2145+ if (
2146+ ! active ||
2147+ result . length < 3 ||
2148+ ( process . env . NODE_ENV !== 'production' &&
2149+ ( ( router as CoordinatorRouter ) . _refreshNextLoad ||
2150+ router . _tx ?. [ 6 /* refresh */ ] ) )
2151+ ) {
2152+ return
2153+ }
2154+ location = result [ 2 /* location */ ] !
2155+ } catch ( cause ) {
2156+ if ( ! isNotFound ( cause ) ) {
2157+ console . error ( cause )
2158+ }
2159+ return
21262160 }
21272161 }
2128- return
21292162}
21302163
21312164// --- SSR hydration (client entry via @tanstack/router-core/ssr/client) ---
0 commit comments