From 1daca754898b0b51d82b36661ad1e4ae1e3c8dd3 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 25 Aug 2025 15:40:47 +0200 Subject: [PATCH] feat: bring back `` component (#3249) This PR brings back the `` component from Fresh 1.x . The implementation is much cleaner than back then and also works client-side now too. --- deno.json | 17 +- docs/canary/advanced/head.md | 76 +++++ docs/canary/examples/migration-guide.md | 47 --- docs/canary/examples/modifying-the-head.md | 195 ----------- docs/toc.ts | 2 +- packages/fresh/src/context.ts | 55 ++- packages/fresh/src/dev/check.ts | 17 +- packages/fresh/src/render.ts | 84 ++--- .../src/runtime/client/preact_hooks_client.ts | 101 +++++- packages/fresh/src/runtime/head.tsx | 11 + .../fresh/src/runtime/server/preact_hooks.tsx | 105 +++++- packages/fresh/src/runtime/shared.ts | 2 + .../fresh/src/runtime/shared_internal.tsx | 31 +- packages/fresh/tests/doc_examples_test.tsx | 17 +- .../tests/fixture_head/islands/MetaIsland.tsx | 18 + .../fixture_head/islands/StyleIdIsland.tsx | 18 + .../fixture_head/islands/TemplateIsland.tsx | 18 + .../fixture_head/islands/TitleIsland.tsx | 22 ++ .../fresh/tests/fixture_head/routes/_app.tsx | 22 ++ .../fresh/tests/fixture_head/routes/id.tsx | 5 + .../fresh/tests/fixture_head/routes/key.tsx | 5 + .../fresh/tests/fixture_head/routes/meta.tsx | 5 + .../fresh/tests/fixture_head/routes/title.tsx | 5 + .../tests/fixture_precompile/valid/deno.json | 17 +- packages/fresh/tests/head_test.tsx | 317 ++++++++++++++++++ packages/init/src/init.ts | 29 +- www/routes/docs/_middleware.ts | 1 + 27 files changed, 916 insertions(+), 326 deletions(-) create mode 100644 docs/canary/advanced/head.md delete mode 100644 docs/canary/examples/modifying-the-head.md create mode 100644 packages/fresh/src/runtime/head.tsx create mode 100644 packages/fresh/tests/fixture_head/islands/MetaIsland.tsx create mode 100644 packages/fresh/tests/fixture_head/islands/StyleIdIsland.tsx create mode 100644 packages/fresh/tests/fixture_head/islands/TemplateIsland.tsx create mode 100644 packages/fresh/tests/fixture_head/islands/TitleIsland.tsx create mode 100644 packages/fresh/tests/fixture_head/routes/_app.tsx create mode 100644 packages/fresh/tests/fixture_head/routes/id.tsx create mode 100644 packages/fresh/tests/fixture_head/routes/key.tsx create mode 100644 packages/fresh/tests/fixture_head/routes/meta.tsx create mode 100644 packages/fresh/tests/fixture_head/routes/title.tsx create mode 100644 packages/fresh/tests/head_test.tsx diff --git a/deno.json b/deno.json index ceb3b9d6..d95a01f3 100644 --- a/deno.json +++ b/deno.json @@ -87,7 +87,22 @@ "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" + ] }, "lint": { "rules": { diff --git a/docs/canary/advanced/head.md b/docs/canary/advanced/head.md new file mode 100644 index 00000000..844c97ce --- /dev/null +++ b/docs/canary/advanced/head.md @@ -0,0 +1,76 @@ +--- +description: Modify the document head in Fresh +--- + +The +[``](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/head)-element +is a crucial element in HTML to set metadata for a page. It allows you to: + +- Set the document title with `` +- Specify page metadata with `<meta>` +- Link to resources like stylesheets with `<link>` +- Include JavaScript code with `<script>` + +> [info]: The outer HTML structure including `<head>` is typically created +> inside `_app.tsx`. + +## Passing metadata from `ctx.state` + +For simple scenarios passing metadata along from a handler or a middleware by +writing to `ctx.state` is often sufficient. + +```tsx routes/_app.tsx +import { define } from "../util.ts"; + +export default define.page((ctx) => { + return ( + <html lang="en"> + <head> + <meta charset="utf-8" /> + <title>{ctx.state.title ?? "Welcome!"} + + + + + + ); +}); +``` + +## Using the ``-component + +For more complex scenarios, or to set page metadata from islands, Fresh ships +with the ``-component. + +```tsx routes/about.tsx +import { Head } from "fresh/runtime"; + +export default define.page((ctx) => { + return ( +
+ + About me + +

About me

+

I like Fresh!

+
+ ); +}); +``` + +### Avoiding duplicate tags + +You might end up with duplicate tags, when multiple `` components are +rendered on the same page. Fresh will employ the following strategies to find +the matching element: + +1. For `` elements Fresh will set `document.title` directly +2. Check if an element with the same `key` exists +3. Check if an element with the same `id` attribute +4. Only for `<meta>` elements: Check if there is a `<meta>` element with the + same `name` attribute +5. No matching element was found, Fresh will create a new one and append it to + `<head>` + +> [info]: The `<title>`-tag is automatically deduplicated, even without a `key` +> prop. diff --git a/docs/canary/examples/migration-guide.md b/docs/canary/examples/migration-guide.md index aac18ac0..1b4757e4 100644 --- a/docs/canary/examples/migration-guide.md +++ b/docs/canary/examples/migration-guide.md @@ -147,53 +147,6 @@ accordingly. } ``` -## Removal of `<Head>` component - -The `<Head>` component was used in Fresh 1.x to add additional tags to the -`<head>` portion of an HTML document from anywhere on the page. This feature was -removed in preparation and due to performance concerns as it required a complex -machinery in the background to work. - -Instead, passing head-related data is best done via `ctx.state`, which can be -easily set through the [define helper](/docs/canary/advanced/define). - -```tsx -// utils.ts -export interface State { - title?: string; -} -export const define = createDefine<State>(); - -// routes/about.tsx -import { define } from "../utils.ts"; - -export default define.page(function AboutPage(ctx) { - // Set a route specific data in a handler - ctx.state.title = "About Me"; - return ( - <div> - <h1>About Me</h1> - </div> - ); -}); - -// Render that in _app.tsx -export default define.page(function App({ Component, state }) { - return ( - <html lang="en"> - <head> - <meta charset="utf-8" /> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - {state.title ? <title>{state.title} : null} - - - - - - ); -}); -``` - ## Update deployment settings Fresh 2 requires assets to be build during deployment instead of building them diff --git a/docs/canary/examples/modifying-the-head.md b/docs/canary/examples/modifying-the-head.md deleted file mode 100644 index a7a8e084..00000000 --- a/docs/canary/examples/modifying-the-head.md +++ /dev/null @@ -1,195 +0,0 @@ ---- -description: | - Modify content by passing state between AppWrapper/Layout and routes. ---- - -It's a common requirement for specific pages to need changes to content inside -the `` tag. - -- Setting the document title using `` -- Specifying page metadata using `<meta>` -- Linking to resources like stylesheets using `<link>` -- Including third-party JavaScript code using `<script>` - -## App Wrapper with State - -The recommended way to modify content in `<head>` is to use an app wrapper in -combination with `Context.state`. - -### 1. State - -The first step can be to define the possible state available on `Context`. In -this example, the `State` is very general to allow very flexible usage. - -```ts util.ts -import { createDefine } from "fresh"; -import type { JSX } from "preact"; - -export interface State { - title?: string; - metas?: JSX.IntrinsicElements["meta"][]; - links?: JSX.IntrinsicElements["link"][]; - scripts?: JSX.IntrinsicElements["script"][]; -} - -export const define = createDefine<State>(); -``` - -### 2. App wrapper - -After defining the State, create the App Wrapper to render the HTML with the -provided State. - -```tsx routes/_app.tsx -import { define } from "../util.ts"; - -export default define.page(function AppWrapper(ctx) { - const { title, links, metas, scripts } = ctx.state; - - return ( - <html lang="en"> - <head> - <meta charset="utf-8" /> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <title>{title ? `${title} | My App` : "My App"} - - {metas?.map((attrs, i) => )} - {links?.map((attrs, i) => )} - {scripts?.map((attrs, i) =>