rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
44 lines (43 loc) • 1.56 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { getManifest } from "../lib/manifest.js";
// context(justinvdm, 2026-03-15): See toManifestKey in stylesheets.tsx for
// why we strip the leading slash.
const toManifestKey = (id) => (id.startsWith("/") ? id.slice(1) : id);
export function findScriptForModule(id, manifest) {
const visited = new Set();
function find(id) {
if (visited.has(id)) {
return;
}
visited.add(id);
const manifestEntry = manifest[toManifestKey(id)];
if (!manifestEntry) {
return;
}
if (manifestEntry.isEntry || manifestEntry.isDynamicEntry) {
return manifestEntry;
}
if (manifestEntry.imports) {
for (const dep of manifestEntry.imports) {
const entry = find(dep);
if (entry) {
return entry;
}
}
}
return;
}
return find(id);
}
import { toAbsoluteHref } from "./assetPaths.js";
export const Preloads = async ({ requestInfo, }) => {
const manifest = await getManifest();
const allScripts = new Set();
for (const scriptId of requestInfo.rw.scriptsToBeLoaded) {
const script = findScriptForModule(scriptId, manifest);
if (script) {
allScripts.add(toAbsoluteHref(script.file));
}
}
return (_jsx(_Fragment, { children: Array.from(allScripts).map((href) => (_jsx("link", { rel: "modulepreload", href: href }, href))) }));
};