rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
40 lines (39 loc) • 1.3 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { getManifest } from "../lib/manifest.js";
export function findScriptForModule(id, manifest) {
const visited = new Set();
function find(id) {
if (visited.has(id)) {
return;
}
visited.add(id);
const manifestEntry = manifest[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);
}
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(script.file);
}
}
return (_jsx(_Fragment, { children: Array.from(allScripts).map((href) => (_jsx("link", { rel: "modulepreload", href: href }, href))) }));
};