Head Management
Set page titles and metadata with head(), middleware, or site-wide defaults. Page values take precedence over middleware and config.
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 HeadConfig: the head fields plus an optional titleTemplate:
// pages/posts/[slug].server.ts
import { defineHandler, defineHead } from 'void';
import type { PageParams } from 'void/routes';
export interface Props {
post: { title: string; excerpt: string; slug: string };
}
export const loader = defineHandler<Props>(async (c) => {
const { slug } = c.req.param() as PageParams<'/posts/[slug]'>;
const post = await getPost(slug);
return { post };
});
export const head = defineHead<Props>((c, props) => {
return {
title: props.post.title,
meta: [
{ name: 'description', content: props.post.excerpt },
{ property: 'og:title', content: props.post.title },
],
};
});head() runs server-side only, after the loader resolves.
Site defaults
Set site-wide head defaults in void.config.ts:
{
"head": {
"titleTemplate": "%s | My Site",
"htmlAttrs": { "lang": "en" },
"meta": [
{ "charset": "utf-8" },
{ "name": "viewport", "content": "width=device-width, initial-scale=1" }
]
}
}titleTemplate wraps the page title. %s is replaced with whatever head() returns as title. A page returning { title: "About" } with the template above produces <title>About | My Site</title>.
Middleware defaults
Set headDefaults in middleware to apply defaults to every page. For example, a theme script can run before first paint:
// middleware/01.head.ts
import { defineMiddleware } from 'void';
export default defineMiddleware(async (c, next) => {
c.set('headDefaults', {
script: [
{
innerHTML: `const t = localStorage.getItem("theme");
if (t) document.documentElement.dataset.theme = t;`,
},
],
});
await next();
});headDefaults also accepts titleTemplate, which replaces the config's template for the requests the middleware runs on. Use it to give one section of the site its own title suffix:
// middleware/admin.ts
import { defineMiddleware } from 'void';
export default defineMiddleware(async (c, next) => {
if (c.req.path.startsWith('/admin')) {
c.set('headDefaults', { ...c.get('headDefaults'), titleTemplate: '%s | Admin' });
}
await next();
});A page returning { title: "Users" } under /admin produces <title>Users | Admin</title>. Other pages keep the config's template.
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</title>.
Page title templates
Our title template adds the site name to every page. The home page already includes it, so we can skip the suffix on that page by returning an empty titleTemplate from head().
// pages/index.server.ts
import { defineHead } from 'void';
export const head = defineHead(() => ({
title: 'My Site | Guides and Tutorials',
titleTemplate: '',
}));The browser tab now reads My Site | Guides and Tutorials. Other pages still use %s | My Site. You can also give a page its own template, such as %s - Docs, to override both the middleware and site defaults.
Precedence
When multiple layers provide head data, they merge with this precedence:
| Field | Behavior |
|---|---|
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. |
htmlAttrs | Shallow merge, page wins conflicts. |
bodyAttrs | Shallow merge, page wins conflicts. |
Social Preview URLs
Use a relative path for a social preview image. Void turns it into an absolute URL using the incoming request:
// void.config.ts
import { defineConfig } from 'void/config';
export default defineConfig({
head: {
meta: [{ property: 'og:image', content: '/og-image.jpg' }],
},
});A request to https://example.com/posts/hello produces:
<meta property="og:image" content="https://example.com/og-image.jpg" />This works in config, middleware, and page head data, including client-side navigation. Void resolves the following tags:
og:image,og:image:url,og:image:secure_urlog:video,og:video:url,og:video:secure_urlog:audio,og:audio:url,og:audio:secure_urlog:urlandtwitter:image
Absolute URLs and other tags keep the values you provide. For prerendered pages, provide an absolute URL such as https://example.com/og-image.jpg: the build does not know your site's origin and leaves relative paths as written.
HeadDescriptor
Page head(), middleware headDefaults and the head config also accept titleTemplate?: string (the HeadConfig type).
interface HeadDescriptor {
title?: string;
meta?: Array<{
name?: string;
property?: string;
content?: string;
charset?: string;
}>;
link?: Array<{ rel: string; href: string; [key: string]: string | undefined }>;
script?: Array<{ src?: string; innerHTML?: string; [key: string]: string | undefined }>;
htmlAttrs?: Record<string, string>;
bodyAttrs?: Record<string, string>;
}Markdown pages
Markdown pages (.md files) generate head tags from frontmatter:
---
title: Getting Started
description: Learn how to use Void
---
Your content here...This produces <title>Getting Started</title> and <meta name="description" content="Learn how to use Void">, with titleTemplate applied if configured.
Client-side updates
Titles, meta tags, and links update automatically on client-side navigation.
script, htmlAttrs, and bodyAttrs are SSR-only.