Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route chunks: Add modules preloads #11954

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the manifest arg having a different name, this function was identical to getModuleLinkHrefs above. I went with manifest rather than manifestPatch as the canonical arg name.

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