Since JavaScript apps are rendered within the document <body>
, they are unable to render markup to the document <head>
, as it's outside of their scope. To help with this, Inertia ships with an <Head>
component, which can be used to set the page <title>
, <meta>
tags, and other <head>
elements.
<Head>
component is not available in the Svelte adapter, as Svelte already ships with a <svelte:head>
component.<Head>
component will only replace <head>
elements that are not in your server-side root template.To add <head>
elements to your page, use the <Head>
component:
import { Head } from '@inertiajs/inertia-vue3'
<Head>
<title>Your page title</title>
<meta name="description" content="Your page description">
</Head>
You can also pass through the page title as a prop to the <Head>
component:
import { Head } from '@inertiajs/inertia-vue3'
<Head title="Your page title" />
You can globally modify the page <title>
using the title
callback in the createInertiaApp
setup method. A common use case for this is automatically adding an app name before or after each page title.
createInertiaApp({
title: title => `${title} - My App`,
// ...
})
Now when you set a title using the <Head>
component, this function will be automatically called.
import { Head } from '@inertiajs/inertia-vue3'
<Head title="Home">
Which, in this example, will result in the following <title>
tag:
<title>Home - My App</title>
This also works if you set the title using a <title>
tag within your <Head>
component:
import { Head } from '@inertiajs/inertia-vue3'
<Head>
<title>Home</title>
</Head>
It's possible to have multiple instances of the <Head>
component throughout your application. For example, your layout can set defaults, and then your pages can overide those defaults.
// Layout.vue
import { Head } from '@inertiajs/inertia-vue3'
<Head>
<title>My app</title>
<meta head-key="description" name="description" content="This is the default description" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
</Head>
// About.vue
import { Head } from '@inertiajs/inertia-vue3'
<Head>
<title>About - My app</title>
<meta head-key="description" name="description" content="This is a page specific description" />
</Head>
Inertia will only ever render one <title>
tag, however all other tags will be stacked, since it's possible to have multiple instances of them. To avoid duplicate tags in your <head>
, you can use the head-key
property, which will make sure the tag is only rendered once. This is illustrated in the above example for the <meta name="description">
tag.
Here is the resulting HTML for this example:
<head>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>About - My app</title>
<meta name="description" content="This is my about page description" />
</head>
In a real app, it can be helpful to create a custom head component that uses the <Head>
component. This gives you a place to set app-wide defaults, such as appending the app name to the page title. Here's a simple example of what this could look like.
<!-- AppHead.vue -->
<template>
<Head :title="title ? `${title} - My App` : 'My App'">
<slot />
</Head>
</template>
<script>
import { Head } from '@inertiajs/inertia-vue3'
export default {
components: {
Head,
},
props: {
title: String,
},
}
</script>
And then use this custom component in your pages:
import AppHead from './AppHead'
<AppHead title="About" />