@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
129 lines (128 loc) • 4.33 kB
JavaScript
import { useRouter } from "./useRouter.js";
import { createComponent, insert, mergeProps, spread, template } from "@solidjs/web";
import { createEffect } from "solid-js";
import { isServer as isServer$1 } from "@tanstack/router-core/isServer";
//#region src/Asset.tsx
var _tmpl$ = /* @__PURE__ */ template(`<meta>`), _tmpl$2 = /* @__PURE__ */ template(`<link>`), _tmpl$3 = /* @__PURE__ */ template(`<style>`), _tmpl$4 = /* @__PURE__ */ template(`<title>`), _tmpl$5 = /* @__PURE__ */ template(`<script>`);
function Asset({ tag, attrs, children }) {
switch (tag) {
case "title": return createComponent(Title, {
attrs,
children
});
case "meta": return (() => {
var _el$ = _tmpl$();
spread(_el$, attrs, false);
return _el$;
})();
case "link": return (() => {
var _el$2 = _tmpl$2();
spread(_el$2, attrs, false);
return _el$2;
})();
case "style":
if (typeof children === "string") return (() => {
var _el$3 = _tmpl$3();
spread(_el$3, mergeProps(attrs, { "innerHTML": children }), false);
return _el$3;
})();
return (() => {
var _el$4 = _tmpl$3();
spread(_el$4, attrs, false);
return _el$4;
})();
case "script": return createComponent(Script, {
attrs,
children
});
default: return null;
}
}
function Title(props) {
const router = useRouter();
const attrs = props.attrs;
const children = props.children;
if (isServer$1 ?? router.isServer) return (() => {
var _el$5 = _tmpl$4();
spread(_el$5, attrs, true);
insert(_el$5, children);
return _el$5;
})();
createEffect(() => children, (titleText) => {
document.title = typeof titleText === "string" ? titleText : "";
});
return (() => {
var _el$6 = _tmpl$4();
spread(_el$6, attrs, true);
insert(_el$6, children);
return _el$6;
})();
}
function Script(props) {
const router = useRouter();
const attrs = props.attrs;
const children = props.children;
const dataScript = typeof attrs?.type === "string" && attrs.type !== "" && attrs.type !== "text/javascript" && attrs.type !== "module";
if (isServer$1 ?? router.isServer) {
if (attrs?.src) return (() => {
var _el$7 = _tmpl$5();
spread(_el$7, attrs, false);
return _el$7;
})();
if (typeof children === "string") return (() => {
var _el$8 = _tmpl$5();
spread(_el$8, mergeProps(attrs, { "innerHTML": children }), false);
return _el$8;
})();
return null;
}
if (dataScript && typeof children === "string") return (() => {
var _el$9 = _tmpl$5();
spread(_el$9, mergeProps(attrs, { "innerHTML": children }), false);
return _el$9;
})();
createEffect(() => ({
attrs,
children,
dataScript
}), ({ attrs, children, dataScript }) => {
if (dataScript) return;
let script;
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;
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);
} else 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;
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);
}
return () => {
if (script?.parentNode) script.parentNode.removeChild(script);
};
});
return null;
}
//#endregion
export { Asset };
//# sourceMappingURL=Asset.js.map