Repository: remix-run/react-router
File: packages/react-router/lib/dom/ssr/single-fetch.tsx (TypeScript)

Code before:
        } catch (e) {
          results[m.route.id] = { type: "error", result: e };
        }
      })
    )
  );
  return results;
}

// Loaders are trickier since we only want to hit the server once, so we
// create a singular promise for all server-loader routes to latch onto.
async function singleFetchLoaderNavigationStrategy(
  manifest: AssetsManifest,
  routeModules: RouteModules,
  ssr: boolean,
  router: DataRouter,
  request: Request,
  matches: DataStrategyFunctionArgs["matches"],
  basename: string | undefined
) {
  // Track which routes need a server load for use in a `_routes` param
  let routesParams = new Set<string>();

  // Only add `_routes` when at least 1 route opts out via `shouldRevalidate`/`clientLoader`
  let foundOptOutRoute = false;

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

  // Deferred we'll use for the singleular call to the server
  let singleFetchDfd = createDeferred<DecodedSingleFetchResults>();

  // Base URL and RequestInit for calls to the server
  let url = stripIndexParam(singleFetchUrl(request.url, basename));
  let init = await createRequestInit(request);

  // We'll build up this results object as we loop through matches
  let results: Record<string, DataStrategyResult> = {};

  let resolvePromise = Promise.all(
    matches.map(async (m, i) =>
      m.resolve(async (handler) => {
        routeDfds[i].resolve();

        let manifestRoute = manifest.routes[m.route.id];
        invariant(manifestRoute, "No manifest route found for dataStrategy");

        if (!m.shouldLoad) {
          // If we're not yet initialized and this is the initial load, respect
          // `shouldLoad` because we're only dealing with `clientLoader.hydrate`
          // routes which will fall into the `clientLoader` section below.

Review comment (suggestion):
I went back to the single deferred approach since we can now avoid dual `fetchAndDecode`/`unwrap` DI needs by decoding to a shared shape in `fetchAndDecode` - so mostly just comment updates leftover

Code after:
      })
    )
  );
  return results;
}

// Loaders are trickier since we only want to hit the server once, so we
// create a singular promise for all server-loader routes to latch onto.
async function singleFetchLoaderNavigationStrategy(
  { request, matches }: DataStrategyFunctionArgs,
  manifest: AssetsManifest,
  ssr: boolean,
  router: DataRouter,
  basename: string | undefined
) {
  // Track which routes need a server load for use in a `_routes` param
  let routesParams = new Set<string>();

  // Only add `_routes` when at least 1 route opts out via `shouldRevalidate`/`clientLoader`
  let foundOptOutRoute = false;

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

  // Deferred we'll use for the singleular call to the server
  let singleFetchDfd = createDeferred<DecodedSingleFetchResults>();

  // Base URL and RequestInit for calls to the server
  let url = stripIndexParam(singleFetchUrl(request.url, basename));
  let init = await createRequestInit(request);

  // We'll build up this results object as we loop through matches
  let results: Record<string, DataStrategyResult> = {};

  let resolvePromise = Promise.all(
    matches.map(async (m, i) =>
      m.resolve(async (handler) => {
        routeDfds[i].resolve();

        let manifestRoute = manifest.routes[m.route.id];
        invariant(manifestRoute, "No manifest route found for dataStrategy");

        let defaultShouldRevalidate =
          !m.unstable_shouldRevalidateArgs ||
          m.unstable_shouldRevalidateArgs.actionStatus == null ||
          m.unstable_shouldRevalidateArgs.actionStatus < 400;
        let shouldCall = m.unstable_shouldCallHandler(defaultShouldRevalidate);

        if (!shouldCall) {
          // If this route opted out, don't include in the .data request
          foundOptOutRoute ||=