UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

86 lines (85 loc) 3.35 kB
import { useRouter } from "./useRouter.js"; import { createComponent, mergeProps, spread, template } from "solid-js/web"; import { onCleanup, onMount } from "solid-js"; import { isServer } from "@tanstack/router-core/isServer"; import { Link, Meta, Style, Title } from "@solidjs/meta"; //#region src/Asset.tsx var _tmpl$ = /* @__PURE__ */ template(`<script>`); function Asset({ tag, attrs, children }) { switch (tag) { case "title": return createComponent(Title, mergeProps(attrs, { children })); case "meta": return createComponent(Meta, attrs); case "link": return createComponent(Link, attrs); case "style": return createComponent(Style, mergeProps(attrs, { children })); case "script": return createComponent(Script, { attrs, children }); default: return null; } } function Script({ attrs, children }) { const router = useRouter(); const dataScript = typeof attrs?.type === "string" && attrs.type !== "" && attrs.type !== "text/javascript" && attrs.type !== "module"; onMount(() => { if (dataScript) return; if (attrs?.src) { const normSrc = (() => { try { const base = document.baseURI || window.location.href; return new URL(attrs.src, base).href; } catch { return attrs.src; } })(); if (Array.from(document.querySelectorAll("script[src]")).find((el) => el.src === normSrc)) return; const script = document.createElement("script"); for (const [key, value] of Object.entries(attrs)) if (value !== void 0 && value !== false) script.setAttribute(key, typeof value === "boolean" ? "" : String(value)); document.head.appendChild(script); onCleanup(() => { if (script.parentNode) script.parentNode.removeChild(script); }); } if (typeof children === "string") { const typeAttr = typeof attrs?.type === "string" ? attrs.type : "text/javascript"; const nonceAttr = typeof attrs?.nonce === "string" ? attrs.nonce : void 0; if (Array.from(document.querySelectorAll("script:not([src])")).find((el) => { if (!(el instanceof HTMLScriptElement)) return false; const sType = el.getAttribute("type") ?? "text/javascript"; const sNonce = el.getAttribute("nonce") ?? void 0; return el.textContent === children && sType === typeAttr && sNonce === nonceAttr; })) return; const script = document.createElement("script"); script.textContent = children; if (attrs) { for (const [key, value] of Object.entries(attrs)) if (value !== void 0 && value !== false) script.setAttribute(key, typeof value === "boolean" ? "" : String(value)); } document.head.appendChild(script); onCleanup(() => { if (script.parentNode) script.parentNode.removeChild(script); }); } }); if (!(isServer ?? router.isServer)) { if (dataScript && typeof children === "string") return (() => { var _el$ = _tmpl$(); spread(_el$, mergeProps(attrs, { "innerHTML": children }), false, false); return _el$; })(); return null; } if (attrs?.src && typeof attrs.src === "string") return (() => { var _el$2 = _tmpl$(); spread(_el$2, attrs, false, false); return _el$2; })(); if (typeof children === "string") return (() => { var _el$3 = _tmpl$(); spread(_el$3, mergeProps(attrs, { "innerHTML": children }), false, false); return _el$3; })(); return null; } //#endregion export { Asset }; //# sourceMappingURL=Asset.js.map