Open History to see the last commit
1import { MetaProvider } from '@solidjs/meta'
2import { For, createEffect, createMemo } from 'solid-js'
3import { DEV_STYLES_ATTR } from '@tanstack/router-core'
4import { Asset } from './Asset'
5import { useHydrated } from './ClientOnly'
6import { useTags } from './headContentUtils'
7import type { HeadContentProps } from './HeadContent'
8β
9/**
10 * @description The `HeadContent` component is used to render meta tags, links, and scripts for the current route.
11 * When using full document hydration (hydrating from `<html>`), this component should be rendered in the `<body>`
12 * to ensure it's part of the reactive tree and updates correctly during client-side navigation.
13 * The component uses portals internally to render content into the `<head>` element.
14 *
15 * Development version: filters out dev styles link after hydration and
16 * includes a fallback cleanup effect for hydration mismatch cases.
17 */
18export function HeadContent(props: HeadContentProps) {
19 const tags = useTags(props.assetCrossOrigin)
20 const hydrated = useHydrated()
21β
22 // Fallback cleanup for hydration mismatch cases
23 // Runs when hydration completes to remove any orphaned dev styles links from DOM
24 createEffect(() => {
25 if (hydrated()) {
26 document
27 .querySelectorAll(`link[${DEV_STYLES_ATTR}]`)
28 .forEach((el) => el.remove())
29 }
30 })
31β
32 // Filter out dev styles after hydration
33 const filteredTags = createMemo(() => {
34 if (hydrated()) {
35 return tags().filter(
36 (tag) => tag.tag !== 'link' || tag.attrs?.[DEV_STYLES_ATTR] !== true,
37 )
38 }
39 return tags()
40 })
41β
42 return (
43 <MetaProvider>
44 <For each={filteredTags()}>{(tag) => <Asset {...tag} />}</For>
45 </MetaProvider>
46 )
47}