Title and meta tags
Title, meta tags, documentHead and htmlAttributes of a page
Write the title and the meta tags of a page in its head export (or in documentHead of the root Layout), never as <title> or <meta> in a component. React 19 allows them in a component and moves them into the head in the browser, but Tifa writes the head of the document as a string and React renders only the app root inside <body>. So the server HTML has the tag in <body>, where crawlers and share cards do not read it, and the page can end up with two descriptions.
// not this
export default function Page() {
return <><title>Pour-over kettle</title><p>...</p></>
}
// this
export const head = () => ({ meta: [{ title: 'Pour-over kettle' }] })
tifa check, tifa build and tifa dev warn about a <title> or <meta> written as JSX, with its file and line. A <title> inside an <svg> is the tooltip of a graphic and is fine, and so is a <meta itemProp>.
The document shell and documentHead
- The document shell (
<html>,<head>, assets and the script that injects data) is written as a string by the framework. React only renders the app root inside<body>, so the page is still a complete document when any level fails or returns 404. The root Layout (app/layout.tsx) can exportdocumentHeadto customize the shell, in the same format as the return value ofhead. Its tags are carried by every page, also when no route matches or the rootLayoutfails, and itshtmlAttributesare the attributes of<html>every page starts from ({ lang: 'en' }when it has none, and a route'sheadcan change them for its pages, see below). Put the site's defaulttitlehere so that the 404 page for an unmatched URL also has a title. A page's ownheadthat returns atitleoverrides it.
export const documentHead = {
htmlAttributes: { lang: 'zh-CN' },
meta: [{ title: 'Shop' }, { name: 'theme-color', content: '#f2f4f5' }],
links: [{ rel: 'icon', href: '/favicon.svg' }],
}
What the head of a page is made of
- A page's head comes from the
headreturn values of the matched routes:titleand meta tags with the same name come from the deepest route that has them, which keeps every one of them it writes (atheme-colorfor eachmedia, severalog:image), and link, style and script tags are deduplicated in route order. The framework always adds<meta charset="utf-8">and a viewport meta, and a route'sname: 'viewport'meta replaces the latter. Tag attributes are written the way React DOM writes them on the server:className,htmlFor,httpEquiv,crossOriginand so on become their HTML names. Event attributes starting withon, invalid attribute names,null,undefinedand functions are not written. Boolean attributes such asasyncanddeferare written with an empty value when true. For other attributes, boolean values are written as strings only ondata-andaria-attributes.javascript:links are replaced. Head tags do not support thestyleattribute, and using it makes rendering fail.htmlAttributesfollows the same rules. The content of style and script tags is written as is. Only the<scriptand</script(in style,<styleand</style) that would end the element are escaped, the way React DOM does it. After browser navigation and afterrouter.invalidate(), the head updates to match the new routes. - A
headcan also returnhtmlAttributes, the attributes of<html>for its pages, such as a theme class or adata-attribute. They are merged over thehtmlAttributesof the root Layout'sdocumentHeadone attribute at a time: the deepest route that gives an attribute wins,undefinedleaves it to the routes above, andnulltakes it away. Attributes are matched by the name React DOM writes, soclassNamein one route andclassin another are the same attribute.classis replaced as a whole, never merged with a value from elsewhere.styleis not supported, as on the tags of the head. They are written by the same rules as the tags of the head. After browser navigation and afterrouter.invalidate()they follow the new routes too. In the browser Tifa writes an attribute of<html>only when the value it wants for it changes, and takes away only attributes it wrote. So what a script of the page sets, such as thedarkclass of a dark mode script or thelanga translation tool changes, stays through hydration and navigation while the routes want the same value of that attribute. When that value changes, because a route sets anotherclassor a navigation leaves it and the root Layout'sclasscomes back, Tifa writes it and the class the script added is gone. Keep such a theme in an attribute no route sets, such asdata-theme, or have the script add it again. A page without SSR gets these attributes in the browser.
// app/guide/layout.tsx. Layout in route.go returns the theme of the guide.
export const head = ({ loaderData }: { loaderData?: LayoutData }) => ({
htmlAttributes: { 'data-theme': loaderData?.theme },
})
- The routes that count are those whose head is on the page. A 404 or an error below the Layout keeps the Layout's attributes, also for a URL below the Layout that no page has, since its 404 shows inside the Layout (see 404). When the Layout itself fails, its
headruns without data, so the example above leaves the root Layout'sdata-theme. - On a site with languages Tifa writes
langitself, the language of the page. AlanginhtmlAttributesfails the build there (see A site in several languages). htmlAttributesmust be an object, andpnpm typecheckreports any other value. At run time such a value fails like an error theheadthrows: the server render fails as an unexpected error (OnError, or a 500), with the message in the log, and on the page undertifa dev. In the browser, after a navigation or on a page without SSR, the router writes the error to the console and drops the whole head of that route.
The head of a 404 or an error page
The page that shows the error gets the head of a page whose Load failed. The route the error replaces runs its head without data, the routes below it add no tags, and the head of every route above it runs again over the matches as they now are, so status tells it that a route failed (see A site in several languages). Tags and htmlAttributes that a route gave for the page that did not render are not on the error page. The browser works out the same head when it hydrates.