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 />
</>
);
}
+198 -56
View File
@@ -4,12 +4,17 @@ import {
buildProd,
parseHtml,
waitFor,
waitForText,
withBrowserApp,
} from "./test_utils.tsx";
import { expect } from "@std/expect";
import { FakeServer } from "../src/test_utils.ts";
import * as path from "@std/path";
const applyHeadCache = await buildProd({
root: path.join(import.meta.dirname!, "fixture_head"),
});
Deno.test("Head - ssr - updates title", async () => {
const handler = new App()
.appWrapper(({ Component }) => {
@@ -151,19 +156,45 @@ Deno.test("Head - ssr - merge keyed", async () => {
expect(last?.textContent).toEqual("ok");
});
Deno.test("Head - ssr - updates link", async () => {
const handler = new App()
.appWrapper(({ Component }) => {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="canonical" href="https://example.com/not-ok" />
</head>
<body>
<Component />
</body>
</html>
);
})
.get("/", (ctx) => {
return ctx.render(
<Head>
<link rel="canonical" href="https://example.com/ok" />
</Head>,
);
}).handler();
const server = new FakeServer(handler);
const res = await server.get("/");
const doc = parseHtml(await res.text());
const link = doc.querySelector("link[rel='canonical']") as HTMLLinkElement;
expect(link.href).toEqual("https://example.com/ok");
});
Deno.test({
ignore: true, // Temporarily until client perf is fixed
name: "Head - client - set title",
fn: async () => {
const applyCache = await buildProd({
root: path.join(import.meta.dirname!, "fixture_head"),
});
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyCache(app);
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/title`);
@@ -171,10 +202,7 @@ Deno.test({
await page.locator(".ready").wait();
await page.locator("button").click();
await waitFor(async () => {
const title = await page.evaluate(() => document.title);
return title === "Count: 1";
});
await waitForText(page, "title", "Count: 1");
});
},
sanitizeOps: false,
@@ -182,18 +210,13 @@ Deno.test({
});
Deno.test({
ignore: true, // Temporarily until client perf is fixed
name: "Head - client - match meta",
fn: async () => {
const applyCache = await buildProd({
root: path.join(import.meta.dirname!, "fixture_head"),
});
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyCache(app);
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/meta`);
@@ -210,15 +233,11 @@ Deno.test({
) => ({ name: el.name, content: el.content }));
});
try {
expect(metas).toEqual([
{ name: "foo", content: "ok" },
{ name: "bar", content: "not ok" },
]);
return true;
} catch {
return false;
}
expect(metas).toEqual([
{ name: "foo", content: "ok" },
{ name: "bar", content: "not ok" },
]);
return true;
});
});
},
@@ -227,18 +246,13 @@ Deno.test({
});
Deno.test({
ignore: true, // Temporarily until client perf is fixed
name: "Head - client - match style by id",
fn: async () => {
const applyCache = await buildProd({
root: path.join(import.meta.dirname!, "fixture_head"),
});
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyCache(app);
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/id`);
@@ -255,15 +269,11 @@ Deno.test({
) => ({ id: el.id, text: el.textContent }));
});
try {
expect(styles).toEqual([
{ id: "", text: "not ok" },
{ id: "style-id", text: "ok" },
]);
return true;
} catch {
return false;
}
expect(styles).toEqual([
{ id: "", text: "not ok" },
{ id: "style-id", text: "ok" },
]);
return true;
});
});
},
@@ -272,18 +282,13 @@ Deno.test({
});
Deno.test({
ignore: true, // Temporarily until client perf is fixed
name: "Head - client - match key",
fn: async () => {
const applyCache = await buildProd({
root: path.join(import.meta.dirname!, "fixture_head"),
});
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyCache(app);
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/key`);
@@ -303,19 +308,156 @@ Deno.test({
}));
});
try {
expect(tpls).toEqual([
{ key: "a", text: "ok" },
{ key: "b", text: "not ok" },
{ key: null, text: "not ok" },
]);
return true;
} catch {
return false;
}
expect(tpls).toEqual([
{ key: "a", text: "ok" },
{ key: "b", text: "not ok" },
{ key: null, text: "not ok" },
]);
return true;
});
});
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Head - client - dynamic meta update",
fn: async () => {
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/dynamic_meta`);
await page.locator(".ready").wait();
// Verify initial meta value
await waitFor(async () => {
const content = await page.evaluate(() => {
const el = document.querySelector(
"meta[name='foo']",
) as HTMLMetaElement;
return el?.content;
});
expect(content).toEqual("value-0");
return true;
});
// Click to update and verify meta changes reactively
await page.locator("button").click();
await waitFor(async () => {
const content = await page.evaluate(() => {
const el = document.querySelector(
"meta[name='foo']",
) as HTMLMetaElement;
return el?.content;
});
expect(content).toEqual("value-1");
return true;
});
});
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Head - client - link element",
fn: async () => {
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/link`);
await page.locator(".ready").wait();
await waitFor(async () => {
const href = await page.evaluate(() => {
const el = document.querySelector(
"link[rel='canonical']",
) as HTMLLinkElement;
return el?.href;
});
expect(href).toEqual("https://example.com/ok");
return true;
});
});
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Head - client - multiple islands with Head",
fn: async () => {
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/multi`);
await page.locator(".ready-a").wait();
await page.locator(".ready-b").wait();
await waitFor(async () => {
const title = await page.evaluate(() => document.title);
expect(title).toEqual("from island A");
return true;
});
await waitFor(async () => {
const metas = await page.evaluate(() => {
return {
author: (document.querySelector(
"meta[name='author']",
) as HTMLMetaElement)?.content,
description: (document.querySelector(
"meta[name='description']",
) as HTMLMetaElement)?.content,
};
});
expect(metas.author).toEqual("island-a");
expect(metas.description).toEqual("from-island-b");
return true;
});
});
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Head - client - title updates multiple times",
fn: async () => {
const app = new App({})
.use(staticFiles())
.fsRoutes();
applyHeadCache(app);
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/title`);
await page.locator(".ready").wait();
await page.locator("button").click();
await waitForText(page, "title", "Count: 1");
await page.locator("button").click();
await waitForText(page, "title", "Count: 2");
await page.locator("button").click();
await waitForText(page, "title", "Count: 3");
});
},
sanitizeOps: false,
sanitizeResources: false,
});