The +Head setting allows you to add <head> tags to your pages.
See Guides > <head> tags for a general introduction and for other ways to add <head> tags.
As explained below, it's only used when rendering the HTML of the first page the user visits. Consequently, it usually cannot be used for setting the <title> tag.
// /pages/+Head.jsx// /pages/+Head.vue// Environment: serverimport previewImage from './previewImage.jpg'import favicon from './favicon.png'import iconMobile from './iconMobile.png'export function Head() { return <> {/* Adding a script tag */} <script type="text/javascript" src="https://example.com/some-script.js"></script> {/* Icon shown in the browser tab (aka favicon) */ <link rel="icon" href={favicon}> {/* Icon shown on mobile homescreens (PWA) */ <link rel="apple-touch-icon" href={iconMobile}> {/* Image shown when sharing on social sites (Twitter, WhatsApp, ...) */} <meta property="og:image" content={previewImage}> </>}
// pages/product/@id/+data.jsximport { useConfig } from 'vike-react/useConfig' // or vike-{vue,solid}export async function data() { const config = useConfig() const data = await fetchSomeData() config({ Head: <> {/* Image shown when sharing on social sites (Twitter, WhatsApp, ...) */} <meta property="og:image" content={data.product.image}> </> })}
For Vue you can use the following:
import { h } from 'vue'config({ Head: h('meta', { property: 'og:image', content: data.product.image })})
useData()/usePageContext() inside +Head
The value defined by +Head is a component and thus you can use useData() and usePageContext() as usual:
// pages/product/@id/+Head.jsimport { useData } from 'vike-react/useData' // or vike-{vue,solid}export function Head() { const data = useData() return <> {/* Image shown when sharing on social sites (Twitter, WhatsApp, ...) */} <meta property="og:image" content={data.product.image}> </>}
Only HTML
Only renders for the first page's HTML
The <Head> component is only used when rendering the HTML of the first page the user visits: the tags set by <Head> aren't updated upon client-side page navigation.
Limitation
The most notable limitation is that the +Head setting cannot be used to set the <title> value, because the title isn't updated when navigating to a page with a different title.
If the user then clicks on a link <a href="/about">About us</a>, then Vike does client-side navigation and the page's title isn't updated: the browser sill shows Welcome even though the URL is now /about. That's because the HTML isn't used upon client-side navigation (DOM manipulations are made instead) while +Head is only used when generating HTML.
The <Head> component is only loaded on the server-side and only used when rendering HTML of the first page by design.
This isn't an issue for <meta name="description"> tag because it's meant for search engines bots which
crawl your website using HTML.
Cumulative
The +Head setting is cumulative. For example:
// /pages/+Head.js// Environment: serverimport favicon from './favicon.png'export const Head = () => // This favicon applies to all pages <link rel="icon" href={favicon}>
// /pages/about-us/+Head.js// Environment: serverimport previewImage from './previewImage.jpg'export const Head = () => // Both the favicon above and this tag applies to /pages/about-us/+Page.js <meta property="og:image" content={previewImage}>
To apply different +Head settings to different pages:
// /pages/(marketing)/+Head.js// Environment: serverimport favicon from './favicon.png'// Applies to all marketing pagesexport const Head = () => <link rel="icon" href={favicon}>
// /pages/admin/+Head.js// Environment: serverimport favicon from './favicon.png'// Applies to all admin pagesexport const Head = () => <link rel="icon" href={favicon}>