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:
Marvin Hagemeister
2025-08-25 15:40:47 +02:00
committed by GitHub
parent bf25d10a84
commit 1daca75489
27 changed files with 916 additions and 326 deletions
@@ -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 />;
}