@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
161 lines (160 loc) • 4.26 kB
JavaScript
import { useRouter } from "./useRouter.js";
import { _getAssetMatches, appendUniqueUserTags, escapeHtml, getAssetCrossOrigin, getScriptPreloadAttrs, resolveManifestCssLink } from "@tanstack/router-core";
import * as Solid from "solid-js";
//#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 = useRouter();
const nonce = router.options.ssr?.nonce;
return Solid.createMemo((prev) => {
const matches = _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: 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 = [];
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: {
...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
}
});
}
appendUniqueUserTags(next, links);
if (manifest) {
for (const match of matches) for (const link of manifest.routes[match.routeId]?.css ?? []) {
const resolvedLink = resolveManifestCssLink(link);
next.push({
tag: "link",
attrs: {
rel: "stylesheet",
...resolvedLink,
crossOrigin: 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
});
}
}
appendUniqueUserTags(next, styles);
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
export { useTags };
//# sourceMappingURL=headContentUtils.js.map