<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 a <Head> component which can be used to set the page <title>, <meta> tags, and other <head> elements.
The <Head> component will only replace <head> elements that are not in your server-side root template.
The <Head> component is not available in the Svelte adapter, as Svelte already ships with its own <svelte:head> component.
Head Component
To add<head> elements to your page, use the <Head> component. Within this component, you can include the elements that you wish to add to the document <head>.
Title Shorthand
If you only need to add a<title> to the document <head>, you may simply pass the title as a prop to the <Head> component.
Title Callback
You can globally modify the page<title> using the title callback in the createInertiaApp setup method. Typically, this method is invoked in your application’s main JavaScript file. A common use case for the title callback is automatically adding an app name before or after each page title.
title callback, the callback will automatically be invoked when you set a title using the <Head> component.
<title> tag.
title callback will also be invoked when you set the title using a <title> tag within your <Head> component.
Multiple Head Instances
It’s possible to have multiple instances of the<Head> component throughout your application. For example, your layout can set some default <Head> elements, and then your individual pages can override those defaults.
<title> tag; however, all other tags will be stacked since it’s valid 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 example above for the <meta name="description"> tag.
The code example above will render the following HTML.
Head Extension
When building a real application, it can sometimes be helpful to create a custom head component that extends Inertia’s<Head> component. This gives you a place to set app-wide defaults, such as appending the app name to the page title.
Server-Side Head Elements
You may wish to include default head elements like<title> or <meta> tags in your root Blade template. This creates two challenges: preventing duplicates when SSR is active, and ensuring the client-side <Head> component may replace them during navigation.
SSR Fallback
The<x-inertia::head> Blade component accepts fallback content via its slot. This content is only rendered in the initial HTML when SSR is not active. When SSR is active, the <Head> component already provides these elements as part of the server-rendered response, so the fallback is skipped.
resources/views/app.blade.php
The data-inertia Attribute
The client-side <Head> component tracks the elements it manages by marking them with a data-inertia attribute. By default, it leaves any other tags in your Blade template untouched. Since there may only be one <title> tag, the <Head> component will always replace it. Other elements like <meta> and <link> may have multiple instances, so the component needs a way to identify which ones it should manage.
You may add the data-inertia attribute to elements in your Blade template to have the <Head> component adopt them. On the first client-side navigation, it will match these elements by key and replace or remove them as needed.
resources/views/app.blade.php
data-inertia value corresponds to the head-key property on your client-side <Head> elements.