Add support for title templates
Description
Currently, when setting page titles in Vike, we have to manually handle title templates for each page. Having built-in support for title templates would improve developer experience and provide a consistent way to handle page titles across the application.
Current Workaround
Currently, we have to manually concatenate titles:
config({
title: `My Site | ${pageContext.config.meta?.title}`,
});
Proposed Solution
Add support for title templates similar to other meta frameworks. For example:
export default {
meta: {
titleTemplate: '%s | My Site', // or (title) => `${title} | My Site`
}
}
Then in page components:
config({
title: 'Articles', // Would automatically become "Articles | My Site"
})
Yes, that's would be nice. It's very much something we want to implement.
Also, I was thinking of adding a new config +name with a default +titleTemplate:
// Default +titleTemplate
function titleTemplate(pageContext) {
const { title, name } = pageContext.config
if (!name || !title) return null
return `${title} | ${name}`
}
The +name config would also set <meta property="og:site_name" content="${pageContext.config.name}">.
Or maybe projectName instead of name to be less ambiguous.
WDYT? (Contribution/PR welcome in case that's something you'd be up to.)
Super... I'm in
Tracked at https://github.com/vikejs/vike/issues/2522.