diff --git a/docs/guide/pages-routing/head.md b/docs/guide/pages-routing/head.md index d262f448a..12376d903 100644 --- a/docs/guide/pages-routing/head.md +++ b/docs/guide/pages-routing/head.md @@ -9,7 +9,7 @@ Set page titles and metadata with `head()`, middleware, or site-wide defaults. P ## Page titles and metadata -Export a `head()` function from your `.server.ts` file. It receives the Hono context and the resolved loader props, and returns a `HeadDescriptor`: +Export a `head()` function from your `.server.ts` file. It receives the Hono context and the resolved loader props, and returns a `HeadConfig`: the [head fields](#headdescriptor) plus an optional [`titleTemplate`](#page-title-templates): ```ts // pages/posts/[slug].server.ts @@ -98,13 +98,29 @@ A page returning `{ title: "Users" }` under `/admin` produces `Users | Ad To drop the suffix entirely for a section, including the config's, set the template to an empty string: `c.set('headDefaults', { ...c.get('headDefaults'), titleTemplate: '' })`. A page returning `{ title: "Users" }` under `/admin` then produces `<title>Users`. +## Page title templates + +A page's `head()` can also return `titleTemplate`. It replaces the template from middleware and config for that page. Use it when a page's title already names the site, such as a home page: + +```ts +// pages/index.server.ts +import { defineHead } from 'void'; + +export const head = defineHead(() => ({ + title: 'My Site | Guides and Tutorials', + titleTemplate: '', // [!code highlight] +})); +``` + +With `"titleTemplate": "%s | My Site"` in config, this page produces `My Site | Guides and Tutorials`, while other pages keep the suffix. An empty string drops the template. Any other value replaces it, so `titleTemplate: '%s - Docs'` gives the page its own suffix. + ## Precedence When multiple layers provide head data, they merge with this precedence: | Field | Behavior | | ----------- | -------------------------------------------------------------------------------------------------------------------- | -| `title` | Page wins. `titleTemplate` from middleware, or else from config, wraps the winning title. | +| `title` | Page wins. `titleTemplate` from the page, or else middleware, or else config, wraps the winning title. | | `meta` | Deduplicates by `name` or `property`. Page entries override matching keys, and non-conflicting entries are appended. | | `link` | Concatenated: config first, then middleware, then page. | | `script` | Concatenated: config first, then middleware, then page. | @@ -143,6 +159,8 @@ Absolute URLs and other tags keep the values you provide. For prerendered pages, ## HeadDescriptor +Page `head()`, middleware `headDefaults` and the `head` config also accept `titleTemplate?: string` (the `HeadConfig` type). + ```ts interface HeadDescriptor { title?: string; diff --git a/docs/reference/api/handlers.md b/docs/reference/api/handlers.md index 1c72ce269..8432e4442 100644 --- a/docs/reference/api/handlers.md +++ b/docs/reference/api/handlers.md @@ -224,7 +224,7 @@ interface RenderAssetTags { ## `defineHead

(handler)` {#definehead-p-handler} -Type-safe wrapper for the page `head()` export in `.server.ts` files. Provides [`CloudContext`](./types.md#cloudcontext) typing for the first argument and generic props typing for the second. +Type-safe wrapper for the page `head()` export in `.server.ts` files. It returns a [`HeadConfig`](./types.md#headconfig). Provides [`CloudContext`](./types.md#cloudcontext) typing for the first argument and generic props typing for the second. ```ts import { defineHandler, defineHead } from 'void'; @@ -254,6 +254,6 @@ export const head = defineHead((c, props) => { ```ts function defineHead

>( - handler: (c: CloudContext, props: P) => HeadDescriptor | undefined, -): (c: CloudContext, props: P) => HeadDescriptor | undefined; + handler: (c: CloudContext, props: P) => HeadConfig | undefined, +): (c: CloudContext, props: P) => HeadConfig | undefined; ``` diff --git a/docs/reference/api/types.md b/docs/reference/api/types.md index 9f3cffe91..c871ac03d 100644 --- a/docs/reference/api/types.md +++ b/docs/reference/api/types.md @@ -72,7 +72,7 @@ interface TypedHandler { ## `HeadDescriptor` {#headdescriptor} -Shape of page head metadata returned by `defineHead`. All fields are optional. +Shape of page head metadata. All fields are optional. ```ts interface HeadDescriptor { @@ -85,6 +85,16 @@ interface HeadDescriptor { } ``` +## `HeadConfig` {#headconfig} + +Head metadata returned by `defineHead`, set as middleware `headDefaults`, or set as `head` in `void.config.ts`. It adds `titleTemplate` to [`HeadDescriptor`](#headdescriptor). `%s` is replaced with the page title. A page's template replaces the middleware's, which replaces the config's. An empty string drops the template. + +```ts +type HeadConfig = HeadDescriptor & { + titleTemplate?: string; +}; +``` + ## `RouteMap` {#routemap} Generated route types used by the typed `fetch` client. Contains each route’s methods, inputs, and outputs. diff --git a/docs/reference/config.md b/docs/reference/config.md index 2b3c012ce..072ea6071 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -192,7 +192,7 @@ Site-wide HTML `` defaults for pages mode. Sets a title template, default | Field | Type | Description | | --------------- | ------------------------ | ---------------------------------------------------------------- | | `title` | `string` | Default page title (overridden by page `head()`) | -| `titleTemplate` | `string` | Wraps the resolved title. `%s` is replaced with the page title. | +| `titleTemplate` | `string` | Wraps the title at `%s`. Page or middleware templates replace it | | `meta` | `Array` | Default `` tags (deduped by `name`/`property`, page wins) | | `link` | `Array` | Default `` tags (concatenated: config, middleware, page) | | `script` | `Array` | Default `