Skip to content

Commit

Permalink
Preload route chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Sep 4, 2024
1 parent 359318e commit ac71173
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
11 changes: 3 additions & 8 deletions packages/react-router/lib/dom/ssr/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,9 @@ import(${JSON.stringify(manifest.entry.module)});`;
// eslint-disable-next-line
}, []);

let routePreloads = matches
.map((match) => {
let route = manifest.routes[match.route.id];
return (route.imports || []).concat([route.module]);
})
.flat(1);

let preloads = isHydrated ? [] : manifest.entry.imports.concat(routePreloads);
let preloads = isHydrated
? []
: manifest.entry.imports.concat(getModuleLinkHrefs(matches, manifest));

return isHydrated ? null : (
<>
Expand Down
31 changes: 7 additions & 24 deletions packages/react-router/lib/dom/ssr/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export function getKeyedLinksForMatches(
})
.flat(2);

let preloads = getCurrentPageModulePreloadHrefs(matches, manifest);
let preloads = getModuleLinkHrefs(matches, manifest);
return dedupeLinkDescriptors(descriptors, preloads);
}

Expand Down Expand Up @@ -421,27 +421,6 @@ export function getNewMatchesForLinks(
}

export function getModuleLinkHrefs(
matches: AgnosticDataRouteMatch[],
manifestPatch: AssetsManifest
): string[] {
return dedupeHrefs(
matches
.map((match) => {
let route = manifestPatch.routes[match.route.id];
let hrefs = [route.module];
if (route.imports) {
hrefs = hrefs.concat(route.imports);
}
return hrefs;
})
.flat(1)
);
}

// The `<Script>` will render rel=modulepreload for the current page, we don't
// need to include them in a page prefetch, this gives us the list to remove
// while deduping.
function getCurrentPageModulePreloadHrefs(
matches: AgnosticDataRouteMatch[],
manifest: AssetsManifest
): string[] {
Expand All @@ -450,11 +429,15 @@ function getCurrentPageModulePreloadHrefs(
.map((match) => {
let route = manifest.routes[match.route.id];
let hrefs = [route.module];

if (route.clientActionModule) {
hrefs = hrefs.concat(route.clientActionModule);
}
if (route.clientLoaderModule) {
hrefs = hrefs.concat(route.clientLoaderModule);
}
if (route.imports) {
hrefs = hrefs.concat(route.imports);
}

return hrefs;
})
.flat(1)
Expand Down

0 comments on commit ac71173

Please sign in to comment.