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,23 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function HeadCounter() {
const [ready, setReady] = useState(false);
const [v, set] = useState(0);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready" : "not-ready"}>
<Head>
<title>Count: {v}</title>
</Head>
<p class="result">Count: {v}</p>
<button type="button" onClick={() => set((v) => v + 1)}>
update
</button>
</div>
);
}
@@ -0,0 +1,20 @@
import { useEffect, useState } from "preact/hooks";
import { Head } from "fresh/runtime";
export function HeadMeta() {
const [ready, setReady] = useState(false);
useEffect(() => {
setReady(true);
}, []);
return (
<div class={ready ? "ready" : "not-ready"}>
<Head>
<meta name="custom" content="ok" />
<meta name="custom-new" content="ok" />
</Head>
<h1>check meta</h1>
</div>
);
}
+16
View File
@@ -0,0 +1,16 @@
import type { PageProps } from "fresh";
export default function App({ Component }: PageProps) {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="custom" content="foo" />
</head>
<body>
<Component />
</body>
</html>
);
}
@@ -0,0 +1,5 @@
import { HeadCounter } from "../../islands/tests/HeadCounter.tsx";
export default function Page() {
return <HeadCounter />;
}
@@ -0,0 +1,5 @@
import { HeadMeta } from "../../islands/tests/HeadMeta.tsx";
export default function Page() {
return <HeadMeta />;
}
+28
View File
@@ -636,3 +636,31 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - client side <Head>",
fn: async () => {
await launchProd(
{ cwd: viteResult.tmp },
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/head_counter`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
await page.locator("button").click();
await waitForText(page, ".result", "Count: 1");
await waitFor(async () => {
const title = await page.evaluate(() => document.title);
expect(title).toEqual("Count: 1");
return true;
});
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
@@ -546,3 +546,47 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite dev - client side <Head>",
fn: async () => {
await withBrowser(async (page) => {
await page.goto(`${demoServer.address()}/tests/head_counter`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
await page.locator("button").click();
await waitForText(page, ".result", "Count: 1");
await waitFor(async () => {
const title = await page.evaluate(() => document.title);
expect(title).toEqual("Count: 1");
return true;
});
await page.goto(`${demoServer.address()}/tests/head_meta`, {
waitUntil: "networkidle2",
});
await page.locator(".ready").wait();
await waitFor(async () => {
const custom = await page
.locator("meta[name='custom']")
// deno-lint-ignore no-explicit-any
.evaluate((el: any) => el.content);
expect(custom).toEqual("ok");
const custom2 = await page
.locator("meta[name='custom-new']")
// deno-lint-ignore no-explicit-any
.evaluate((el: any) => el.content);
expect(custom2).toEqual("ok");
return true;
});
});
},
sanitizeOps: false,
sanitizeResources: false,
});