@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
163 lines (162 loc) • 4.57 kB
JavaScript
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let solid_js = require("solid-js");
solid_js = require_runtime.__toESM(solid_js, 1);
//#region src/headContentUtils.tsx
/**
* Build the head/link/meta/script tags from the renderable presented prefix.
* Used internally by `HeadContent`.
*/
var useTags = (assetCrossOrigin) => {
const router = require_useRouter.useRouter();
const nonce = router.options.ssr?.nonce;
return solid_js.createMemo((prev) => {
const matches = (0, _tanstack_router_core._getAssetMatches)(router.stores.matches.get());
const resultMeta = [];
const metaByAttribute = {};
let title;
for (let i = matches.length - 1; i >= 0; i--) {
const metas = matches[i].meta;
if (!metas) continue;
for (let j = metas.length - 1; j >= 0; j--) {
const m = metas[j];
if (!m) continue;
if (m.title) {
if (!title) title = {
tag: "title",
children: m.title
};
} else if ("script:ld+json" in m) try {
const json = JSON.stringify(m["script:ld+json"]);
resultMeta.push({
tag: "script",
attrs: { type: "application/ld+json" },
children: (0, _tanstack_router_core.escapeHtml)(json)
});
} catch {}
else {
const attribute = m.name ?? m.property;
if (attribute) if (metaByAttribute[attribute]) continue;
else metaByAttribute[attribute] = true;
resultMeta.push({
tag: "meta",
attrs: {
...m,
nonce
}
});
}
}
}
if (title) resultMeta.push(title);
if (router.options.ssr?.nonce) resultMeta.push({
tag: "meta",
attrs: {
property: "csp-nonce",
content: router.options.ssr.nonce
}
});
resultMeta.reverse();
const next = [];
(0, _tanstack_router_core.appendUniqueUserTags)(next, resultMeta);
const manifest = router.ssr?.manifest;
const preloads = [];
for (const match of matches) for (const preload of manifest?.routes[match.routeId]?.preloads ?? []) {
if (!preload) continue;
preloads.push({
tag: "link",
attrs: {
...(0, _tanstack_router_core.getScriptPreloadAttrs)(manifest, preload, assetCrossOrigin),
nonce
}
});
}
next.push(...preloads);
const links = [];
for (const match of matches) for (const link of match.links ?? []) {
if (link === void 0) continue;
links.push({
tag: "link",
attrs: {
...link,
nonce
}
});
}
(0, _tanstack_router_core.appendUniqueUserTags)(next, links);
if (manifest) {
for (const match of matches) for (const link of manifest.routes[match.routeId]?.css ?? []) {
const resolvedLink = (0, _tanstack_router_core.resolveManifestCssLink)(link);
next.push({
tag: "link",
attrs: {
rel: "stylesheet",
...resolvedLink,
crossOrigin: (0, _tanstack_router_core.getAssetCrossOrigin)(assetCrossOrigin, "stylesheet") ?? resolvedLink.crossOrigin,
nonce
}
});
}
if (manifest.inlineStyle) next.push({
tag: "style",
attrs: {
...manifest.inlineStyle.attrs,
nonce
},
children: manifest.inlineStyle.children,
inlineCss: true
});
}
const styles = [];
const headScripts = [];
for (const match of matches) {
for (const style of match.styles ?? []) {
if (style === void 0) continue;
const { children, ...attrs } = style;
styles.push({
tag: "style",
attrs: {
...attrs,
nonce
},
children
});
}
for (const script of match.headScripts ?? []) {
if (script === void 0) continue;
const { children, ...attrs } = script;
headScripts.push({
tag: "script",
attrs: {
...attrs,
nonce
},
children
});
}
}
(0, _tanstack_router_core.appendUniqueUserTags)(next, styles);
(0, _tanstack_router_core.appendUniqueUserTags)(next, headScripts);
if (prev === void 0) return next;
return replaceEqualTags(prev, next);
});
};
function replaceEqualTags(prev, next) {
const prevByKey = /* @__PURE__ */ new Map();
for (const tag of prev) prevByKey.set(JSON.stringify(tag), tag);
let isEqual = prev.length === next.length;
const result = next.map((tag, index) => {
const existing = prevByKey.get(JSON.stringify(tag));
if (existing) {
if (existing !== prev[index]) isEqual = false;
return existing;
}
isEqual = false;
return tag;
});
return isEqual ? prev : result;
}
//#endregion
exports.useTags = useTags;
//# sourceMappingURL=headContentUtils.cjs.map