@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
130 lines (129 loc) • 4.86 kB
JavaScript
require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
let _solidjs_web = require("@solidjs/web");
let solid_js = require("solid-js");
let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer");
//#region src/Asset.tsx
var _tmpl$ = /* @__PURE__ */ (0, _solidjs_web.template)(`<meta>`), _tmpl$2 = /* @__PURE__ */ (0, _solidjs_web.template)(`<link>`), _tmpl$3 = /* @__PURE__ */ (0, _solidjs_web.template)(`<style>`), _tmpl$4 = /* @__PURE__ */ (0, _solidjs_web.template)(`<title>`), _tmpl$5 = /* @__PURE__ */ (0, _solidjs_web.template)(`<script>`);
function Asset({ tag, attrs, children }) {
switch (tag) {
case "title": return (0, _solidjs_web.createComponent)(Title, {
attrs,
children
});
case "meta": return (() => {
var _el$ = _tmpl$();
(0, _solidjs_web.spread)(_el$, attrs, false);
return _el$;
})();
case "link": return (() => {
var _el$2 = _tmpl$2();
(0, _solidjs_web.spread)(_el$2, attrs, false);
return _el$2;
})();
case "style":
if (typeof children === "string") return (() => {
var _el$3 = _tmpl$3();
(0, _solidjs_web.spread)(_el$3, (0, _solidjs_web.mergeProps)(attrs, { "innerHTML": children }), false);
return _el$3;
})();
return (() => {
var _el$4 = _tmpl$3();
(0, _solidjs_web.spread)(_el$4, attrs, false);
return _el$4;
})();
case "script": return (0, _solidjs_web.createComponent)(Script, {
attrs,
children
});
default: return null;
}
}
function Title(props) {
const router = require_useRouter.useRouter();
const attrs = props.attrs;
const children = props.children;
if (_tanstack_router_core_isServer.isServer ?? router.isServer) return (() => {
var _el$5 = _tmpl$4();
(0, _solidjs_web.spread)(_el$5, attrs, true);
(0, _solidjs_web.insert)(_el$5, children);
return _el$5;
})();
(0, solid_js.createEffect)(() => children, (titleText) => {
document.title = typeof titleText === "string" ? titleText : "";
});
return (() => {
var _el$6 = _tmpl$4();
(0, _solidjs_web.spread)(_el$6, attrs, true);
(0, _solidjs_web.insert)(_el$6, children);
return _el$6;
})();
}
function Script(props) {
const router = require_useRouter.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 (_tanstack_router_core_isServer.isServer ?? router.isServer) {
if (attrs?.src) return (() => {
var _el$7 = _tmpl$5();
(0, _solidjs_web.spread)(_el$7, attrs, false);
return _el$7;
})();
if (typeof children === "string") return (() => {
var _el$8 = _tmpl$5();
(0, _solidjs_web.spread)(_el$8, (0, _solidjs_web.mergeProps)(attrs, { "innerHTML": children }), false);
return _el$8;
})();
return null;
}
if (dataScript && typeof children === "string") return (() => {
var _el$9 = _tmpl$5();
(0, _solidjs_web.spread)(_el$9, (0, _solidjs_web.mergeProps)(attrs, { "innerHTML": children }), false);
return _el$9;
})();
(0, solid_js.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
exports.Asset = Asset;
//# sourceMappingURL=Asset.cjs.map