Head
Kind: cumulative.
Environment: server.
Implemented by: vike-react
/vike-vue
/vike-solid
.
You need
vike-react
/vike-vue
/vike-solid
to be able to use this setting.
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.
Using data
You can set <head>
tags based on fetched data (or pageContext
) by using:
<Head>
inside components
You can use the <Head>
component inside your components:
Here
data
comes from the props passed from the parent component, but it can also come from a data-fetching component hook such asconst { data } = useSuspenseQuery()
when usingvike-react-query
.
It also works inside
.vue
files (when usingvike-vue
).
useConfig()
inside +data
You can use the useConfig()
universal hook inside your +data
hook.
For Vue you can use the following:
useData()
/usePageContext()
inside +Head
The value defined by +Head
is a component and thus you can use useData()
and usePageContext()
as usual:
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 tilte.
See example below for a more detailed explanation.
Instead use the +title
setting.
For use cases where the
+Head
setting cannot be used, Vike offers tailored settings that update upon client-side navigation.
A small limitation
This may seem like a major limitation but it actually isn't: you can use the +Head
setting for the vast majority of use cases.
You can use +Head
for setting <head>
tags are read by HTML crawlers:
- Tags for social sites (Twitter, Instagram, ...) such as
<meta property="og:image">
(the preview image upon URL sharing).Social site bots navigate your website only by using HTML requests: they don't execute client-side JavaScript and don't do client-side navigation.
- Tags for SEO such as
<meta name="description">
.While Google can do client-side navigation, it still discovers
<head>
tags by using its HTML crawler.
You can use +Head
for setting <head>
tags that are global (they have the same value for all pages):
- Favicon.
Assuming all your pages share the same favicon (
<link rel="icon">
), there isn't any need to update the favicon upon client-side navigation. - PWA settings.
PWA settings are global and there isn't any need to update them upon client-side navigation.
<script>
Assuming the script applies to all your pages.
Example
The following example showcases that using +Head
for setting <title>
doesn't work, while it does work for setting <meta name="description">
.
If the first URL the user visits is /
then the rendered HTML is:
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:
To apply different +Head
settings to different pages:
If you have a need for overriding, then add a comment at: #1692 - Add
override
anddefault
options for cumulative configs
How to inject raw HTML?
You can inject any arbitrary HTML string to the page's <head>
. Examples using:
⚠️Be cautious about the security risk called XSS injections.
React
You can use React's dangerouslySetInnerHTML
to add raw HTML, for example:
Vue
You can use innerHTML
to add raw HTML, for example:
Solid
You can use innerHTML
to add raw HTML, for example: