UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

185 lines (184 loc) 6.08 kB
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); let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer"); //#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; const activeMatches = solid_js.createMemo(() => (0, _tanstack_router_core._getAssetMatches)(router.stores.matches.get())); const routeMeta = solid_js.createMemo(() => activeMatches().map((match) => match.meta).filter((meta) => meta !== void 0)); const meta = solid_js.createMemo(() => { const resultMeta = []; const metaByAttribute = {}; let title; const routeMetasArray = routeMeta(); for (let i = routeMetasArray.length - 1; i >= 0; i--) { const metas = routeMetasArray[i]; 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; metaByAttribute[attribute] = true; } resultMeta.push({ tag: "meta", attrs: { ...m, nonce } }); } } } if (title) resultMeta.push(title); if (nonce) resultMeta.push({ tag: "meta", attrs: { property: "csp-nonce", content: nonce } }); resultMeta.reverse(); return resultMeta; }); const links = solid_js.createMemo(() => { return activeMatches().flatMap((match) => match.links ?? []).filter((link) => link !== void 0).map((link) => ({ tag: "link", attrs: { ...link, nonce } })); }); const retainedManifestCssTags = /* @__PURE__ */ new Map(); const manifestCssTags = solid_js.createMemo(() => { const manifest = router.ssr?.manifest; const tags = []; if (!manifest) return tags; for (const match of activeMatches()) for (const link of manifest.routes[match.routeId]?.css ?? []) { const resolvedLink = (0, _tanstack_router_core.resolveManifestCssLink)(link); const tag = { tag: "link", attrs: { rel: "stylesheet", ...resolvedLink, crossOrigin: (0, _tanstack_router_core.getAssetCrossOrigin)(assetCrossOrigin, "stylesheet") ?? resolvedLink.crossOrigin, nonce } }; const key = JSON.stringify(tag); if (!retainedManifestCssTags.has(key)) retainedManifestCssTags.set(key, tag); } tags.push(...retainedManifestCssTags.values()); if (manifest.inlineStyle) tags.push({ tag: "style", attrs: { ...manifest.inlineStyle.attrs, nonce }, children: manifest.inlineStyle.children, inlineCss: true }); return tags; }); const preloadLinks = solid_js.createMemo(() => { const manifest = router.ssr?.manifest; const tags = []; for (const match of activeMatches()) for (const preload of manifest?.routes[match.routeId]?.preloads ?? []) { if (!preload) continue; tags.push({ tag: "link", attrs: { ...(0, _tanstack_router_core.getScriptPreloadAttrs)(manifest, preload, assetCrossOrigin), nonce } }); } return tags; }); const styles = solid_js.createMemo(() => { return activeMatches().flatMap((match) => match.styles ?? []).filter((style) => style !== void 0).map(({ children, ...attrs }) => ({ tag: "style", attrs: { ...attrs, nonce }, children })); }); const headScripts = solid_js.createMemo(() => { return activeMatches().flatMap((match) => match.headScripts ?? []).filter((script) => script !== void 0).map(({ children, ...attrs }) => ({ tag: "script", attrs: { ...attrs, nonce }, children })); }); return solid_js.createMemo(() => { const next = []; (0, _tanstack_router_core.appendUniqueUserTags)(next, meta()); (0, _tanstack_router_core.appendUniqueUserTags)(next, links()); next.push(...manifestCssTags()); next.push(...preloadLinks()); (0, _tanstack_router_core.appendUniqueUserTags)(next, styles()); (0, _tanstack_router_core.appendUniqueUserTags)(next, headScripts()); return next; }); }; var INLINE_CSS_HYDRATION_ATTR = "data-tsr-inline-css"; /** * Convert the router-managed tags into head registry descriptors for * `useHead`. Pure data mapping — it is evaluated inside the registry's own * flush boundary (server) / effect (client), so it must not allocate * reactive owners. */ function toHeadTags(tags) { return tags.map(toHeadTag); } function toHeadTag(t) { const props = { ...t.attrs }; let children = t.children; if (t.tag === "style" && t.inlineCss && (process.env.TSS_INLINE_CSS_ENABLED === "true" || process.env.TSS_INLINE_CSS_ENABLED === void 0 && _tanstack_router_core_isServer.isServer)) props[INLINE_CSS_HYDRATION_ATTR] = ""; if (t.tag === "style" && t.inlineCss && children === void 0 && typeof document !== "undefined") children = document.querySelector(`style[${INLINE_CSS_HYDRATION_ATTR}]`)?.textContent ?? ""; if (children !== void 0) props.children = children; const headTag = { tag: t.tag, props }; if (t.tag === "style" || t.tag === "script" && props.src === void 0) headTag.key = `tsr-${hashString(t.tag + JSON.stringify(props))}`; return headTag; } function hashString(s) { let h = 5381; for (let i = 0; i < s.length; i++) h = (h << 5) + h + s.charCodeAt(i) | 0; return (h >>> 0).toString(36); } //#endregion exports.toHeadTags = toHeadTags; exports.useTags = useTags; //# sourceMappingURL=headContentUtils.cjs.map