@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
60 lines (59 loc) • 1.97 kB
JavaScript
import { useHydrated } from "./ClientOnly.js";
import { useRouter } from "./useRouter.js";
import { Asset } from "./Asset.js";
import { useTags } from "./headContentUtils.js";
import { Portal, createComponent, isServer } from "@solidjs/web";
import { For, createEffect, createMemo } from "solid-js";
//#region src/HeadContent.dev.tsx
var DEV_STYLES_ATTR = "data-tanstack-router-dev-styles";
/**
* @description The `HeadContent` component is used to render meta tags, links, and scripts for the current route.
* When using full document hydration (hydrating from `<html>`), this component should be rendered in the `<body>`
* to ensure it's part of the reactive tree and updates correctly during client-side navigation.
* The component uses portals internally to render content into the `<head>` element.
*
* Development version: filters out dev styles link after hydration and
* includes a fallback cleanup effect for hydration mismatch cases.
*/
function HeadContent(props) {
const tags = useTags(props.assetCrossOrigin);
const hydrated = useHydrated();
const router = useRouter();
createEffect(() => [hydrated()], ([hydrated]) => {
if (hydrated) document.querySelectorAll(`link[${DEV_STYLES_ATTR}]`).forEach((el) => el.remove());
});
const filteredTags = createMemo(() => {
if (hydrated()) return tags().filter((tag) => !tag.attrs?.[DEV_STYLES_ATTR]);
return tags();
});
const content = () => createComponent(For, {
get each() {
return filteredTags();
},
children: (tag) => {
const t = tag();
return createComponent(Asset, {
get tag() {
return t.tag;
},
get attrs() {
return t.attrs;
},
get children() {
return t.children;
}
});
}
});
return isServer ?? router.isServer ? content() : createComponent(Portal, {
get mount() {
return document.head;
},
get children() {
return content();
}
});
}
//#endregion
export { HeadContent };
//# sourceMappingURL=HeadContent.dev.js.map