Skip to content

<Head> ​

Sets the document's <title> and other head tags while the component is rendered, in the browser and during server rendering.

gts
import { Head } from 'inertia-ember';

<template>
  <Head @title='Posts'>
    <meta name='description' content='Everything we wrote' head-key='description' />
  </Head>
</template>

Arguments ​

ArgumentTypeDefaultDescription
@titlestringnoneThe page's <title>. It goes through the title callback you gave createInertiaApp. A <title> in the block takes its place.

Blocks ​

The default block holds the head tags: <meta>, <link>, <title>, <script> and so on. It yields nothing.

Give a tag a head-key to make it replaceable: when two rendered <Head>s have a tag with the same key, the one rendered later wins. On a navigation that's the new page, so a page's description replaces the one its layout set. Tags without a head-key are all kept. head-key is a known attribute to Glint.

Element and attributes ​

<Head> renders a hidden <inertia-head> element in place, holding the tags, and copies them into the document head. It takes no ...attributes. In the head, each tag gets data-inertia="<head-key>", which is how Inertia tells its tags apart from the ones your HTML template wrote. A tag that already has data-inertia keeps it.

The tags are watched: when an argument or a value inside the block changes, the document head updates. When the <Head> is destroyed (you leave the page), its tags are removed.

Examples ​

A title from the page's props ​

The server sends a post prop:

gts
import type { TOC } from '@ember/component/template-only';
import { Head, type PageSignature } from 'inertia-ember';

interface Post {
  title: string;
  excerpt: string;
}

export default <template>
  <Head @title={{@props.post.title}}>
    <meta name='description' content={{@props.post.excerpt}} head-key='description' />
  </Head>

  <h1>{{@props.post.title}}</h1>
</template> satisfies TOC<PageSignature<{ post: Post }>>;

The same suffix on every title ​

createInertiaApp's title callback formats every title, whether it comes from @title or a <title> tag:

ts
import { createInertiaApp } from 'inertia-ember';

createInertiaApp({
  title: (title) => (title ? `${title} · My app` : 'My app'),
});

With <Head @title='Posts'> the document title is Posts · My app.

Defaults in the layout, overrides in the page ​

The layout sets a description and social tags for every page. A page that renders its own tag with the same head-key replaces it; the others stay.

gts
// layouts/AppLayout.gts
import type { TOC } from '@ember/component/template-only';
import { Head, type LayoutSignature } from 'inertia-ember';

export default <template>
  <Head>
    <meta name='description' content='A blog about Ember' head-key='description' />
    <meta property='og:site_name' content='My blog' head-key='og:site_name' />
  </Head>

  <main>{{yield}}</main>
</template> satisfies TOC<LayoutSignature>;
gts
// pages/Posts/Show.gts
import { Head } from 'inertia-ember';

<template>
  <Head @title={{@props.post.title}}>
    <meta name='description' content={{@props.post.excerpt}} head-key='description' />
    <link rel='canonical' href={{@props.post.url}} head-key='canonical' />
  </Head>
</template>

Put the layout's <Head> before {{yield}}, so the page's renders after it and wins on the first load too.

A title that changes on its own ​

@title is tracked like any argument, so a getter works:

gts
import Component from '@glimmer/component';
import { Head, type PageSignature } from 'inertia-ember';

export default class Inbox extends Component<PageSignature<{ unread: number }>> {
  get title() {
    return this.args.props.unread > 0 ? `Inbox (${this.args.props.unread})` : 'Inbox';
  }

  <template><Head @title={{this.title}} /></template>
}

Notes ​

  • <Head> needs createInertiaApp() to have run first; otherwise it throws.
  • During server rendering the tags are read from the rendered markup and go into the response's head, so crawlers and link previews see them. The hidden <inertia-head> stays in the body so the browser can hydrate the same markup.
  • @title is escaped. Tags in the block are copied as they render.

Related: Pages and layouts, createInertiaApp (title and serverHead), Server-side rendering, and Inertia's title and meta docs.

Released under the MIT License.