Skip to content

Commit cd5681b

Browse files
authored
Slight refactor of fetchAndDecode for RSC (#13409)
1 parent 7bc2422 commit cd5681b

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

packages/react-router/lib/dom/ssr/single-fetch.tsx

+20-25
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type GetRouteInfoFunction = (routeId: string) => {
154154
};
155155

156156
type FetchAndDecodeFunction = (
157-
request: Request,
157+
args: DataStrategyFunctionArgs,
158158
basename: string | undefined,
159159
targetRoutes?: string[]
160160
) => Promise<{ status: number; data: DecodedSingleFetchResults }>;
@@ -233,12 +233,7 @@ export function getSingleFetchDataStrategyImpl(
233233

234234
// Fetcher loads are singular calls to one loader
235235
if (fetcherKey) {
236-
return singleFetchLoaderFetcherStrategy(
237-
request,
238-
matches,
239-
fetchAndDecode,
240-
basename
241-
);
236+
return singleFetchLoaderFetcherStrategy(args, fetchAndDecode, basename);
242237
}
243238

244239
// Navigational loads are more complex...
@@ -256,16 +251,16 @@ export function getSingleFetchDataStrategyImpl(
256251
// Actions are simple since they're singular calls to the server for both
257252
// navigations and fetchers)
258253
async function singleFetchActionStrategy(
259-
{ request, matches }: DataStrategyFunctionArgs,
254+
args: DataStrategyFunctionArgs,
260255
fetchAndDecode: FetchAndDecodeFunction,
261256
basename: string | undefined
262257
) {
263-
let actionMatch = matches.find((m) => m.unstable_shouldCallHandler());
258+
let actionMatch = args.matches.find((m) => m.unstable_shouldCallHandler());
264259
invariant(actionMatch, "No action match found");
265260
let actionStatus: number | undefined = undefined;
266261
let result = await actionMatch.resolve(async (handler) => {
267262
let result = await handler(async () => {
268-
let { data, status } = await fetchAndDecode(request, basename, [
263+
let { data, status } = await fetchAndDecode(args, basename, [
269264
actionMatch!.route.id,
270265
]);
271266
actionStatus = status;
@@ -290,12 +285,14 @@ async function singleFetchActionStrategy(
290285

291286
// We want to opt-out of Single Fetch when we aren't in SSR mode
292287
async function nonSsrStrategy(
293-
{ request, matches }: DataStrategyFunctionArgs,
288+
args: DataStrategyFunctionArgs,
294289
getRouteInfo: GetRouteInfoFunction,
295290
fetchAndDecode: FetchAndDecodeFunction,
296291
basename: string | undefined
297292
) {
298-
let matchesToLoad = matches.filter((m) => m.unstable_shouldCallHandler());
293+
let matchesToLoad = args.matches.filter((m) =>
294+
m.unstable_shouldCallHandler()
295+
);
299296
let results: Record<string, DataStrategyResult> = {};
300297
await Promise.all(
301298
matchesToLoad.map((m) =>
@@ -308,9 +305,7 @@ async function nonSsrStrategy(
308305
let routeId = m.route.id;
309306
let result = hasClientLoader
310307
? await handler(async () => {
311-
let { data } = await fetchAndDecode(request, basename, [
312-
routeId,
313-
]);
308+
let { data } = await fetchAndDecode(args, basename, [routeId]);
314309
return unwrapSingleFetchResult(data, routeId);
315310
})
316311
: await handler();
@@ -327,7 +322,7 @@ async function nonSsrStrategy(
327322
// Loaders are trickier since we only want to hit the server once, so we
328323
// create a singular promise for all server-loader routes to latch onto.
329324
async function singleFetchLoaderNavigationStrategy(
330-
{ request, matches }: DataStrategyFunctionArgs,
325+
args: DataStrategyFunctionArgs,
331326
router: DataRouter,
332327
getRouteInfo: GetRouteInfoFunction,
333328
fetchAndDecode: FetchAndDecodeFunction,
@@ -341,7 +336,7 @@ async function singleFetchLoaderNavigationStrategy(
341336
let foundOptOutRoute = false;
342337

343338
// Deferreds per-route so we can be sure they've all loaded via `match.resolve()`
344-
let routeDfds = matches.map(() => createDeferred<void>());
339+
let routeDfds = args.matches.map(() => createDeferred<void>());
345340

346341
// Deferred we'll use for the singleular call to the server
347342
let singleFetchDfd = createDeferred<DecodedSingleFetchResults>();
@@ -350,7 +345,7 @@ async function singleFetchLoaderNavigationStrategy(
350345
let results: Record<string, DataStrategyResult> = {};
351346

352347
let resolvePromise = Promise.all(
353-
matches.map(async (m, i) =>
348+
args.matches.map(async (m, i) =>
354349
m.resolve(async (handler) => {
355350
routeDfds[i].resolve();
356351
let routeId = m.route.id;
@@ -380,7 +375,7 @@ async function singleFetchLoaderNavigationStrategy(
380375
}
381376
try {
382377
let result = await handler(async () => {
383-
let { data } = await fetchAndDecode(request, basename, [routeId]);
378+
let { data } = await fetchAndDecode(args, basename, [routeId]);
384379
return unwrapSingleFetchResult(data, routeId);
385380
});
386381

@@ -433,7 +428,7 @@ async function singleFetchLoaderNavigationStrategy(
433428
? [...routesParams.keys()]
434429
: undefined;
435430
try {
436-
let data = await fetchAndDecode(request, basename, targetRoutes);
431+
let data = await fetchAndDecode(args, basename, targetRoutes);
437432
singleFetchDfd.resolve(data.data);
438433
} catch (e) {
439434
singleFetchDfd.reject(e);
@@ -447,17 +442,16 @@ async function singleFetchLoaderNavigationStrategy(
447442

448443
// Fetcher loader calls are much simpler than navigational loader calls
449444
async function singleFetchLoaderFetcherStrategy(
450-
request: Request,
451-
matches: DataStrategyFunctionArgs["matches"],
445+
args: DataStrategyFunctionArgs,
452446
fetchAndDecode: FetchAndDecodeFunction,
453447
basename: string | undefined
454448
) {
455-
let fetcherMatch = matches.find((m) => m.unstable_shouldCallHandler());
449+
let fetcherMatch = args.matches.find((m) => m.unstable_shouldCallHandler());
456450
invariant(fetcherMatch, "No fetcher match found");
457451
let routeId = fetcherMatch.route.id;
458452
let result = await fetcherMatch.resolve(async (handler) =>
459453
handler(async () => {
460-
let { data } = await fetchAndDecode(request, basename, [routeId]);
454+
let { data } = await fetchAndDecode(args, basename, [routeId]);
461455
return unwrapSingleFetchResult(data, routeId);
462456
})
463457
);
@@ -508,10 +502,11 @@ export function singleFetchUrl(
508502
}
509503

510504
async function fetchAndDecodeViaTurboStream(
511-
request: Request,
505+
args: DataStrategyFunctionArgs,
512506
basename: string | undefined,
513507
targetRoutes?: string[]
514508
): Promise<{ status: number; data: DecodedSingleFetchResults }> {
509+
let { request } = args;
515510
let url = singleFetchUrl(request.url, basename);
516511
if (request.method === "GET") {
517512
url = stripIndexParam(url);

0 commit comments

Comments
 (0)