Skip to content

feat(pages): allow titleTemplate in middleware headDefaults - #797

Merged
cpojer merged 5 commits into
voidzero-dev:mainfrom
karthickeyan-dev:middleware-title-template
Nov 13, 2026
Merged

cpojer merged 5 commits into
voidzero-dev:mainfrom
karthickeyan-dev:middleware-title-template

Conversation

@karthickeyan-dev

Copy link
Copy Markdown
Contributor

Closes #796

Summary

titleTemplate could only be set once, for the whole site, in void.config.ts. headDefaults was typed HeadDescriptor, which has no titleTemplate, and resolveHead() read the template only from the config. So one section of a site couldn't have its own title suffix.

  • packages/void/src/runtime/handler.ts: headDefaults is now typed HeadConfig, so c.set('headDefaults', { titleTemplate }) type-checks.
  • packages/void/src/pages/head.ts: resolveHead() uses the middleware's titleTemplate and falls back to the config's. The resolved head still doesn't include titleTemplate.
  • packages/void/src/pages/protocol.ts: the three headDefaults reads cast to HeadConfig.
  • docs/guide/pages-routing/head.md: an example under Middleware defaults, and the title row in the precedence table.
// 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 now renders <title>Users | Admin</title>. Sites that don't set titleTemplate in middleware see no change.

Tests

  • pages-head.test.ts: the middleware template wins over the config's, applies when the config has none, falls back to the config's when middleware doesn't set one, and isn't emitted in the resolved head.
  • routing-runtime.test.ts: a defineMiddleware on /admin/* sets the template, and createPageGetHandler returns Users | Admin under /admin and Users | My Site elsewhere.

4 of the new cases fail without the fix. Without the fix, the runtime test also fails to type-check (TS2769 on c.set).

Verification

  • vp run check: 33/33 pass
  • vp test run: 792 files pass, 1 skipped (16,676 tests pass, 37 skipped)

🤖 Generated with Claude Code

…ev#796.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@0froq 0froq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several additions in the inline comments. The rest looks right. Thank you!

const title = pageHead?.title ?? middlewareHead?.title ?? configHead?.title;
if (title !== undefined) {
const template = configHead?.titleTemplate;
const template = middlewareHead?.titleTemplate ?? configHead?.titleTemplate;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to drop the site-wide suffix for a section. titleTemplate: '' already does it, since the empty string is falsy and template ? ... : title returns the bare title, but that is load-bearing by accident. Switch the check to template !== undefined and the behaviour disappears while every test stays green.

Two small additions would cover it:

  • one case in pages-head.test.ts: { titleTemplate: '' } against a config template resolves to Users.
  • one line in head.md saying an empty template disables the wrapper

I placed them in suggestions

user: AuthUser | null;
session: AuthSession | null;
headDefaults?: HeadDescriptor;
headDefaults?: HeadConfig;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HeadConfig isn't exported from void while HeadDescriptor is (src/index.ts). Anyone wrapping defineMiddleware for a typed helper has to cast or redeclare the shape. Exporting it next to HeadDescriptor keeps the new public type nameable.

Comment thread docs/guide/pages-routing/head.md Resolved
karthickeyan-dev and others added 3 commits November 13, 2026 06:49
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: froQ <sayhola@froq.me>
Signed-off-by: Christoph Nakazawa <xnakazawa@pm.me>
Co-authored-by: froQ <sayhola@froq.me>
Signed-off-by: Christoph Nakazawa <xnakazawa@pm.me>

@0froq 0froq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@0froq
0froq self-requested a review November 13, 2026 01:30

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cpojer
cpojer merged commit 4bdd425 into voidzero-dev:main Nov 13, 2026
2 checks passed
@karthickeyan-dev
karthickeyan-dev deleted the middleware-title-template branch November 14, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow titleTemplate in middleware's headDefaults

3 participants