feat: bring back <Head> component (#3249)
This PR brings back the `<Head>` component from Fresh 1.x . The implementation is much cleaner than back then and also works client-side now too.
This commit is contained in:
@@ -33,7 +33,22 @@ Deno.test("Docs Code example checks", async () => {
|
||||
lib: ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"],
|
||||
jsx: "precompile",
|
||||
jsxImportSource: "preact",
|
||||
jsxPrecompileSkipElements: ["a", "img", "source", "body", "html", "head"],
|
||||
jsxPrecompileSkipElements: [
|
||||
"a",
|
||||
"img",
|
||||
"source",
|
||||
"body",
|
||||
"html",
|
||||
"head",
|
||||
"title",
|
||||
"meta",
|
||||
"script",
|
||||
"link",
|
||||
"style",
|
||||
"base",
|
||||
"noscript",
|
||||
"template",
|
||||
],
|
||||
},
|
||||
};
|
||||
await Deno.writeTextFile(
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { Head } from "fresh/runtime";
|
||||
|
||||
export function MetaIsland() {
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setReady(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div class={ready ? "ready" : ""}>
|
||||
<Head>
|
||||
<meta name="foo" content="ok" />
|
||||
</Head>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { Head } from "fresh/runtime";
|
||||
|
||||
export function StyleIdIsland() {
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setReady(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div class={ready ? "ready" : ""}>
|
||||
<Head>
|
||||
<style id="style-id">ok</style>
|
||||
</Head>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { Head } from "fresh/runtime";
|
||||
|
||||
export function TemplateIsland() {
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setReady(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div class={ready ? "ready" : ""}>
|
||||
<Head>
|
||||
<template key="a">ok</template>
|
||||
</Head>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { Head } from "fresh/runtime";
|
||||
|
||||
export function TitleIsland() {
|
||||
const [s, set] = useState(0);
|
||||
const [ready, setReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setReady(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div class={ready ? "ready" : ""}>
|
||||
<Head>
|
||||
<title>Count: {s}</title>
|
||||
</Head>
|
||||
<button type="button" onClick={() => set((v) => v + 1)}>
|
||||
update {s}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { PageProps } from "@fresh/core";
|
||||
|
||||
export default function Page({ Component }: PageProps) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>not ok</title>
|
||||
<meta name="foo" content="not ok" />
|
||||
<meta name="bar" content="not ok" />
|
||||
<style>not ok</style>
|
||||
<style id="style-id">not ok</style>
|
||||
<template key="a">not ok</template>
|
||||
<template key="b">not ok</template>
|
||||
<template>not ok</template>
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { StyleIdIsland } from "../islands/StyleIdIsland.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <StyleIdIsland />;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { TemplateIsland } from "../islands/TemplateIsland.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <TemplateIsland />;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { MetaIsland } from "../islands/MetaIsland.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <MetaIsland />;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { TitleIsland } from "../islands/TitleIsland.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <TitleIsland />;
|
||||
}
|
||||
@@ -5,6 +5,21 @@
|
||||
"compilerOptions": {
|
||||
"jsx": "precompile",
|
||||
"jsxImportSource": "preact",
|
||||
"jsxPrecompileSkipElements": ["a", "img", "source", "body", "html", "head"]
|
||||
"jsxPrecompileSkipElements": [
|
||||
"a",
|
||||
"img",
|
||||
"source",
|
||||
"body",
|
||||
"html",
|
||||
"head",
|
||||
"title",
|
||||
"meta",
|
||||
"script",
|
||||
"link",
|
||||
"style",
|
||||
"base",
|
||||
"noscript",
|
||||
"template"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
import { App, staticFiles } from "fresh";
|
||||
import { Head } from "fresh/runtime";
|
||||
import {
|
||||
buildProd,
|
||||
parseHtml,
|
||||
waitFor,
|
||||
withBrowserApp,
|
||||
} from "./test_utils.tsx";
|
||||
import { expect } from "@std/expect";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
import * as path from "@std/path";
|
||||
|
||||
Deno.test("Head - ssr - updates title", async () => {
|
||||
const handler = new App()
|
||||
.appWrapper(({ Component }) => {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>not ok</title>
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<>
|
||||
<h1>heading</h1>
|
||||
<Head>
|
||||
<title>ok</title>
|
||||
</Head>
|
||||
</>,
|
||||
);
|
||||
}).handler();
|
||||
|
||||
const server = new FakeServer(handler);
|
||||
const res = await server.get("/");
|
||||
|
||||
const doc = parseHtml(await res.text());
|
||||
|
||||
expect(doc.querySelector("title")?.textContent).toEqual("ok");
|
||||
expect(doc.querySelector("h1")?.textContent).toEqual("heading");
|
||||
});
|
||||
|
||||
Deno.test("Head - ssr - updates meta", async () => {
|
||||
const handler = new App()
|
||||
.appWrapper(({ Component }) => {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>ok</title>
|
||||
<meta name="foo" content="not ok" />
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<>
|
||||
<h1>heading</h1>
|
||||
<Head>
|
||||
<meta name="foo" content="ok" />
|
||||
</Head>
|
||||
</>,
|
||||
);
|
||||
}).handler();
|
||||
|
||||
const server = new FakeServer(handler);
|
||||
const res = await server.get("/");
|
||||
const doc = parseHtml(await res.text());
|
||||
|
||||
const meta = doc.querySelector("meta[name='foo']") as HTMLMetaElement;
|
||||
expect(meta.content).toEqual("ok");
|
||||
});
|
||||
|
||||
Deno.test("Head - ssr - updates style", async () => {
|
||||
const handler = new App()
|
||||
.appWrapper(({ Component }) => {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>ok</title>
|
||||
<meta name="foo" content="not ok" />
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<>
|
||||
<h1>heading</h1>
|
||||
<Head>
|
||||
<style>ok</style>
|
||||
</Head>
|
||||
</>,
|
||||
);
|
||||
}).handler();
|
||||
|
||||
const server = new FakeServer(handler);
|
||||
const res = await server.get("/");
|
||||
const doc = parseHtml(await res.text());
|
||||
|
||||
const meta = doc.querySelector("style") as HTMLStyleElement;
|
||||
expect(meta.textContent).toEqual("ok");
|
||||
});
|
||||
|
||||
Deno.test("Head - ssr - merge keyed", async () => {
|
||||
const handler = new App()
|
||||
.appWrapper(({ Component }) => {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>ok</title>
|
||||
<style>not ok</style>
|
||||
<style key="a">not ok</style>
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<>
|
||||
<h1>heading</h1>
|
||||
<Head>
|
||||
<style key="a">ok</style>
|
||||
</Head>
|
||||
</>,
|
||||
);
|
||||
}).handler();
|
||||
|
||||
const server = new FakeServer(handler);
|
||||
const res = await server.get("/");
|
||||
const doc = parseHtml(await res.text());
|
||||
|
||||
const last = doc.head.lastChild;
|
||||
expect(last?.textContent).toEqual("ok");
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
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);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/title`);
|
||||
|
||||
await page.locator(".ready").wait();
|
||||
await page.locator("button").click();
|
||||
|
||||
await waitFor(async () => {
|
||||
const title = await page.evaluate(() => document.title);
|
||||
return title === "Count: 1";
|
||||
});
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
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);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/meta`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const metas = await page.evaluate(() => {
|
||||
const metas = Array.from(
|
||||
document.querySelectorAll("meta[name]"),
|
||||
) as HTMLMetaElement[];
|
||||
|
||||
return metas.map((
|
||||
el: HTMLMetaElement,
|
||||
) => ({ name: el.name, content: el.content }));
|
||||
});
|
||||
|
||||
try {
|
||||
expect(metas).toEqual([
|
||||
{ name: "foo", content: "ok" },
|
||||
{ name: "bar", content: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
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);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/id`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const styles = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head style"),
|
||||
) as HTMLStyleElement[];
|
||||
|
||||
return els.map((
|
||||
el: HTMLStyleElement,
|
||||
) => ({ id: el.id, text: el.textContent }));
|
||||
});
|
||||
|
||||
try {
|
||||
expect(styles).toEqual([
|
||||
{ id: "", text: "not ok" },
|
||||
{ id: "style-id", text: "ok" },
|
||||
]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
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);
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(`${address}/key`);
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await waitFor(async () => {
|
||||
const tpls = await page.evaluate(() => {
|
||||
const els = Array.from(
|
||||
document.querySelectorAll("head template"),
|
||||
) as HTMLTemplateElement[];
|
||||
|
||||
return els.map((
|
||||
el: HTMLTemplateElement,
|
||||
) => ({
|
||||
key: el.getAttribute("data-key"),
|
||||
text: el.content.textContent,
|
||||
}));
|
||||
});
|
||||
|
||||
try {
|
||||
expect(tpls).toEqual([
|
||||
{ key: "a", text: "ok" },
|
||||
{ key: "b", text: "not ok" },
|
||||
{ key: null, text: "not ok" },
|
||||
]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
Reference in New Issue
Block a user