@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
54 lines (53 loc) • 1.69 kB
JavaScript
import { useRouter } from "./useRouter.js";
import { Asset } from "./Asset.js";
import { NoHydration, createComponent } from "@solidjs/web";
import * as Solid from "solid-js";
//#region src/Scripts.tsx
var Scripts = () => {
const router = useRouter();
const nonce = router.options.ssr?.nonce;
const activeMatches = Solid.createMemo(() => router.stores.activeMatchesSnapshot.state);
const assetScripts = Solid.createMemo(() => {
const assetScripts = [];
const manifest = router.ssr?.manifest;
if (!manifest) return [];
activeMatches().map((match) => router.looseRoutesById[match.routeId]).forEach((route) => manifest.routes[route.id]?.assets?.filter((d) => d.tag === "script").forEach((asset) => {
assetScripts.push({
tag: "script",
attrs: {
...asset.attrs,
nonce
},
children: asset.children
});
}));
return assetScripts;
});
const scripts = Solid.createMemo(() => activeMatches().map((match) => match.scripts).flat(1).filter(Boolean).map(({ children, ...script }) => ({
tag: "script",
attrs: {
...script,
nonce
},
children
})));
const serverBufferedScript = router.serverSsr ? router.serverSsr.takeBufferedScripts() : void 0;
const allScripts = Solid.createMemo(() => {
const result = [...scripts(), ...assetScripts()];
if (serverBufferedScript) result.unshift(serverBufferedScript);
return result;
});
return createComponent(NoHydration, { get children() {
return createComponent(Solid.For, {
get each() {
return allScripts();
},
children: (asset) => {
return createComponent(Asset, Solid.untrack(asset));
}
});
} });
};
//#endregion
export { Scripts };
//# sourceMappingURL=Scripts.js.map