@@ -15,9 +15,11 @@ import {
15
15
stripBasename ,
16
16
} from "../../router/utils" ;
17
17
import { createRequestInit } from "./data" ;
18
- import type { EntryContext } from "./entry" ;
18
+ import type { AssetsManifest , EntryContext } from "./entry" ;
19
19
import { escapeHtml } from "./markup" ;
20
20
import invariant from "./invariant" ;
21
+ import type { RouteModules } from "./routeModules" ;
22
+ import type { DataRouteMatch } from "../../context" ;
21
23
22
24
export const SingleFetchRedirectSymbol = Symbol ( "SingleFetchRedirect" ) ;
23
25
@@ -147,10 +149,10 @@ export function StreamTransfer({
147
149
}
148
150
}
149
151
150
- type GetRouteInfoFunction = ( routeId : string ) => {
152
+ type GetRouteInfoFunction = ( match : DataRouteMatch ) => {
151
153
hasLoader : boolean ;
152
- hasClientLoader : boolean ; // TODO: Can this be read from match.route?
153
- hasShouldRevalidate : boolean | undefined ; // TODO: Can this be read from match.route?
154
+ hasClientLoader : boolean ;
155
+ hasShouldRevalidate : boolean ;
154
156
} ;
155
157
156
158
type FetchAndDecodeFunction = (
@@ -159,23 +161,33 @@ type FetchAndDecodeFunction = (
159
161
targetRoutes ?: string [ ]
160
162
) => Promise < { status : number ; data : DecodedSingleFetchResults } > ;
161
163
162
- export function getSingleFetchDataStrategy (
164
+ export function getTurboStreamSingleFetchDataStrategy (
163
165
getRouter : ( ) => DataRouter ,
164
- getRouteInfo : GetRouteInfoFunction ,
166
+ manifest : AssetsManifest ,
167
+ routeModules : RouteModules ,
165
168
ssr : boolean ,
166
169
basename : string | undefined
167
170
) : DataStrategyFunction {
168
- let dataStrategy = getSingleFetchDataStrategyImpl (
171
+ let dataStrategy = getTurboStreamSingleFetchDataStrategyImpl (
169
172
getRouter ,
170
- getRouteInfo ,
173
+ ( match : DataRouteMatch ) => {
174
+ let manifestRoute = manifest . routes [ match . route . id ] ;
175
+ invariant ( manifestRoute , "Route not found in manifest" ) ;
176
+ let routeModule = routeModules [ match . route . id ] ;
177
+ return {
178
+ hasLoader : manifestRoute . hasLoader ,
179
+ hasClientLoader : manifestRoute . hasClientLoader ,
180
+ hasShouldRevalidate : Boolean ( routeModule ?. shouldRevalidate ) ,
181
+ } ;
182
+ } ,
171
183
fetchAndDecodeViaTurboStream ,
172
184
ssr ,
173
185
basename
174
186
) ;
175
187
return async ( args ) => args . unstable_runClientMiddleware ( dataStrategy ) ;
176
188
}
177
189
178
- export function getSingleFetchDataStrategyImpl (
190
+ export function getTurboStreamSingleFetchDataStrategyImpl (
179
191
getRouter : ( ) => DataRouter ,
180
192
getRouteInfo : GetRouteInfoFunction ,
181
193
fetchAndDecode : FetchAndDecodeFunction ,
@@ -192,7 +204,7 @@ export function getSingleFetchDataStrategyImpl(
192
204
}
193
205
194
206
let foundRevalidatingServerLoader = matches . some ( ( m ) => {
195
- let { hasLoader, hasClientLoader } = getRouteInfo ( m . route . id ) ;
207
+ let { hasLoader, hasClientLoader } = getRouteInfo ( m ) ;
196
208
return m . unstable_shouldCallHandler ( ) && hasLoader && ! hasClientLoader ;
197
209
} ) ;
198
210
if ( ! ssr && ! foundRevalidatingServerLoader ) {
@@ -298,7 +310,7 @@ async function nonSsrStrategy(
298
310
matchesToLoad . map ( ( m ) =>
299
311
m . resolve ( async ( handler ) => {
300
312
try {
301
- let { hasClientLoader } = getRouteInfo ( m . route . id ) ;
313
+ let { hasClientLoader } = getRouteInfo ( m ) ;
302
314
// Need to pass through a `singleFetch` override handler so
303
315
// clientLoader's can still call server loaders through `.data`
304
316
// requests
@@ -350,7 +362,7 @@ async function singleFetchLoaderNavigationStrategy(
350
362
routeDfds [ i ] . resolve ( ) ;
351
363
let routeId = m . route . id ;
352
364
let { hasLoader, hasClientLoader, hasShouldRevalidate } =
353
- getRouteInfo ( routeId ) ;
365
+ getRouteInfo ( m ) ;
354
366
355
367
let defaultShouldRevalidate =
356
368
! m . unstable_shouldRevalidateArgs ||
@@ -419,7 +431,7 @@ async function singleFetchLoaderNavigationStrategy(
419
431
( ! router . state . initialized || routesParams . size === 0 ) &&
420
432
! window . __reactRouterHdrActive
421
433
) {
422
- singleFetchDfd . resolve ( { } ) ;
434
+ singleFetchDfd . resolve ( { routes : { } } ) ;
423
435
} else {
424
436
// When routes have opted out, add a `_routes` param to filter server loaders
425
437
// Skipped in `ssr:false` because we expect to be loading static `.data` files
@@ -659,8 +671,8 @@ function unwrapSingleFetchResult(
659
671
}
660
672
661
673
function createDeferred < T = unknown > ( ) {
662
- let resolve : ( val ?: any ) => Promise < void > ;
663
- let reject : ( error ? : unknown ) => Promise < void > ;
674
+ let resolve : ( val : T ) => Promise < void > ;
675
+ let reject : ( error : unknown ) => Promise < void > ;
664
676
let promise = new Promise < T > ( ( res , rej ) => {
665
677
resolve = async ( val : T ) => {
666
678
res ( val ) ;
0 commit comments