vike icon indicating copy to clipboard operation
vike copied to clipboard

Add support for title templates

Open abdrahmanrizk opened this issue 11 months ago • 2 comments

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"
})

abdrahmanrizk avatar Feb 07 '25 10:02 abdrahmanrizk

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.)

brillout avatar Feb 07 '25 14:02 brillout

Super... I'm in

ghost avatar Apr 15 '25 18:04 ghost

Tracked at https://github.com/vikejs/vike/issues/2522.

brillout avatar Jun 26 '25 16:06 brillout