fix: make <Head> work in client (#3252)

Closes #3310

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marvin Hagemeister
2026-03-29 21:21:39 +02:00
committed by GitHub
co-authored by Bartek Iwańczuk Claude Opus 4.6
parent 19d3589b9b
commit a79b155e79
19 changed files with 557 additions and 131 deletions
@@ -0,0 +1,22 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function DynamicMetaIsland() {
const [count, setCount] = useState(0);
const [ready, setReady] = useState(false);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready" : ""}>
<Head>
<meta name="foo" content={`value-${count}`} />
</Head>
<button type="button" onClick={() => setCount((v) => v + 1)}>
update
</button>
</div>
);
}
@@ -0,0 +1,18 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function LinkIsland() {
const [ready, setReady] = useState(false);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready" : ""}>
<Head>
<link rel="canonical" href="https://example.com/ok" />
</Head>
</div>
);
}
@@ -0,0 +1,19 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function MultiHeadA() {
const [ready, setReady] = useState(false);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready-a" : ""}>
<Head>
<title>from island A</title>
<meta name="author" content="island-a" />
</Head>
</div>
);
}
@@ -0,0 +1,18 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function MultiHeadB() {
const [ready, setReady] = useState(false);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready-b" : ""}>
<Head>
<meta name="description" content="from-island-b" />
</Head>
</div>
);
}
@@ -8,6 +8,7 @@ export default function Page({ Component }: PageProps) {
<title>not ok</title>
<meta name="foo" content="not ok" />
<meta name="bar" content="not ok" />
<link rel="canonical" href="https://example.com/not-ok" />
<style>not ok</style>
<style id="style-id">not ok</style>
<template key="a">not ok</template>
@@ -0,0 +1,5 @@
import { DynamicMetaIsland } from "../islands/DynamicMetaIsland.tsx";
export default function Page() {
return <DynamicMetaIsland />;
}
@@ -0,0 +1,5 @@
import { LinkIsland } from "../islands/LinkIsland.tsx";
export default function Page() {
return <LinkIsland />;
}
@@ -0,0 +1,11 @@
import { MultiHeadA } from "../islands/MultiHeadA.tsx";
import { MultiHeadB } from "../islands/MultiHeadB.tsx";
export default function Page() {
return (
<>
<MultiHeadA />
<MultiHeadB />
</>
);
}