@tanstack/react-router
Version:
Modern and scalable routing for React applications
62 lines (61 loc) • 2.6 kB
JavaScript
require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
const require_Asset = require("./Asset.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");
let _tanstack_react_store = require("@tanstack/react-store");
let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer");
//#region src/Scripts.tsx
/**
* Render body script tags collected from route matches and SSR manifests.
* Should be placed near the end of the document body.
*/
var Scripts = () => {
const router = require_useRouter.useRouter();
const nonce = router.options.ssr?.nonce;
const getAssetScripts = (matches) => {
const assetScripts = [];
const manifest = router.ssr?.manifest;
if (!manifest) return [];
matches.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 getScripts = (matches) => matches.map((match) => match.scripts).flat(1).filter(Boolean).map(({ children, ...script }) => ({
tag: "script",
attrs: {
...script,
suppressHydrationWarning: true,
nonce
},
children
}));
if (_tanstack_router_core_isServer.isServer ?? router.isServer) {
const assetScripts = getAssetScripts(router.stores.activeMatchesSnapshot.state);
return renderScripts(router, getScripts(router.stores.activeMatchesSnapshot.state), assetScripts);
}
const assetScripts = (0, _tanstack_react_store.useStore)(router.stores.activeMatchesSnapshot, getAssetScripts, _tanstack_router_core.deepEqual);
return renderScripts(router, (0, _tanstack_react_store.useStore)(router.stores.activeMatchesSnapshot, getScripts, _tanstack_router_core.deepEqual), assetScripts);
};
function renderScripts(router, scripts, assetScripts) {
let serverBufferedScript = void 0;
if (router.serverSsr) serverBufferedScript = router.serverSsr.takeBufferedScripts();
const allScripts = [...scripts, ...assetScripts];
if (serverBufferedScript) allScripts.unshift(serverBufferedScript);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: allScripts.map((asset, i) => /* @__PURE__ */ (0, react.createElement)(require_Asset.Asset, {
...asset,
key: `tsr-scripts-${asset.tag}-${i}`
})) });
}
//#endregion
exports.Scripts = Scripts;
//# sourceMappingURL=Scripts.cjs.map