Skip to content

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

Open
karthickeyan-dev wants to merge 1 commit into
voidzero-dev:mainfrom
karthickeyan-dev:middleware-title-template
Open

karthickeyan-dev wants to merge 1 commit 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>

This branch has not been deployed

No deployments
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

1 participant