Skip to content

Titles and head

Each page sets its own document title. Kiln reads the title on the server and writes it into the <title> tag, and in the browser it keeps document.title current as you navigate. This guide covers the three ways to set a title, the fallback title, and how the head is assembled.

<kiln:title>

The simplest way is the <kiln:title> tag, which is available in any page file:

tsx
export default function Pricing() {
  return (
    <>
      <kiln:title>Pricing · My app</kiln:title>
      <h1>Pricing</h1>
    </>
  );
}

The Kiln Vite plugin rewrites the tag to <Title> from @kiln-js/core before it compiles the file. The title text is taken from the children as a string. Only text and numbers are kept, so use a plain string, or a template string, for the title.

tsx
<kiln:title>{`Post: ${data.title}`}</kiln:title>

Elements inside the tag, such as <strong>, are ignored when the title is read.

<Title> from core

If you prefer an explicit import, use the component directly:

tsx
import { Title } from "@kiln-js/core";

export default function Pricing() {
  return <Title>Pricing · My app</Title>;
}

It behaves the same way.

Markdown titles

A Markdown page sets its title from the title key in the frontmatter:

markdown
---
title: Getting started
---

# Getting started

See Markdown pages.

The fallback title

When a page sets no title, the server uses the defaultTitle option of createAppHandler, which defaults to Kiln App:

ts
createAppHandler({ assets, defaultTitle: "My app" });

In the browser, a navigation to a page with no title resets document.title to the title the document had when the app started, so a page without a title shows that default. Set a title on every page that needs a specific one.

Where the title goes

On the server, the title is written into the template's <title> element. If the template has no <title>, Kiln adds one before </head>. The title text is escaped, so a title containing < or & is safe.

Titles that come from the page's data are escaped too. You do not need to escape them yourself.

Adding other head tags

Kiln's default template has a <head> section where the stylesheet links go. To add tags such as <meta name="description">, edit the template you pass to createAppHandler:

ts
import { createAppHandler, defaultTemplate } from "@kiln-js/server";

const template = defaultTemplate.replace(
  "</head>",
  `<meta name="description" content="My app" />\n</head>`
);

createAppHandler({ assets, template });

The template must keep the placeholder comments <!--kiln-head-->, <!--kiln-app-->, and <!--kiln-data-->. Without them, the stylesheets, the page HTML, or the client script will not be inserted.

Per-page meta tags such as descriptions are not part of the title system. Set the site-wide ones in the template. Titles are the only per-page head value that Kiln manages.

Titles and streaming

On a .server page the title must be known before the document shell is sent. Kiln reads it from the shell that React produces first, so a title set at the top of the page is used. A title set only after slow data arrives would be too late, so set the title in the part of the page that renders immediately.