Head details
The rules behind the Head guide. HeadInput lists every key of useHead.
Which tag wins
Entries stack in layers. For the same key a later layer wins, so it does not matter when an entry was added.
| Order | Layer |
|---|---|
| 1 | the site defaults (SEO) |
| 2 | app.head of the config |
| 3 | the icon and image files |
| 4 | entries an app plugin adds |
| 5 | the head of each matched route, outermost first |
| 6 | useHead and useSeoMeta in components: the root component, then the layout, then the page |
| Tag | Its key |
|---|---|
| meta | name, property or http-equiv |
| link | rel="canonical", else rel and href |
| script | id or src |
| any tag | key, when you set it |
| a tag with none of these | none; it is never merged |
htmlAttrs and bodyAttrs merge per attribute. useHead removes its entries when the component unmounts, and a render that never reaches the screen leaves no tags.
Defaults
Every page starts with <meta charset="utf-8"> and a viewport meta. app.head.meta is layered over them: a viewport of your own replaces the default one, and other tags leave both in place.
Title template
The last title and the last titleTemplate set win, and the template applies to the final title.
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
app: {
head: {
titleTemplate: "%s · My app",
meta: [{ name: "description", content: "My app" }],
},
},
})
- A template in the config applies to every title. One that only a layout sets does not reach the error page, which has no layout.
- In
useHeadthe template may be a function. It getsundefinedwhen there is no title, so it can return a default. - A string template with no title writes a title only when it uses a template param.
import { useHead } from "@hamur/adapter-react"
export default function Landing() {
useHead({ titleTemplate: (title) => (title ? `${title} | Shop` : "Shop") })
return <h1>Welcome</h1>
}
Template params
templateParams fill %name and %name.sub in a title, a string titleTemplate and every meta content. They merge across entries per key. An unset param stays as written.
| Param | Value |
|---|---|
%s | the title |
%separator | a vertical bar by default; repeated ones become one, and one at either end goes |
%site.name, %site.url, %site.description | the site config |
import { useHead } from "@hamur/adapter-react"
export default function Post() {
useHead({
title: "Anvil",
templateParams: { separator: "·", site: { name: "Forge" } },
titleTemplate: "%s %separator %site.name",
link: [{ rel: "preload", href: "/hero.avif", as: "image", tagPriority: "high" }],
script: [{ src: "/chat.js", async: "", tagPosition: "bodyClose", tagPriority: "low" }],
noscript: [{ innerHTML: '<img src="/pixel.gif" alt="">', tagPosition: "bodyOpen" }],
jsonLd: { "@type": "Product", name: "Anvil" },
})
return <h1>Anvil</h1>
}
Attributes of <html> and <body>
Attributes belong to the entry that set them. A page with htmlAttrs: { lang: "tr" } gives up lang when you go to a page that does not set it. Attributes of your own index.html are put back to their value instead.
The server lists the names it wrote in data-hamur-head-attrs, which the browser reads once and removes.
Repeated tags
A meta tag keeps one value per key, the last. These may repeat within one entry, in order: og:*, article:*, book:*, profile:*, author:*, theme-color, google-site-verification, twitter:image and twitter:image:*.
useHead({
meta: [
{ property: "og:image", content: "/a.png" },
{ property: "og:image", content: "/b.png" },
],
})
og:image:widthand the like belong to theog:imagebefore them. The same holds forog:video,og:audioandtwitter:image.- Another entry that sets
og:imagereplaces the whole group, with its widths and alt texts. - One without its root in the same entry is dropped (HAMUR_W3005).
- A tag with its own
keyis never part of a group.
Search and social tags
SeoMetaInput lists the keys of useSeoMeta and the tags they write.
Safe input
useHeadSafe lists what it keeps and what it drops. hamur dev logs each drop once, without the value (HAMUR_W3006).
Server-only tags
useServerHead and useServerSeoMeta write tags for what only a crawler or a link preview reads, such as site verification. The browser never runs the call, and the client build removes its arguments.
import { useServerSeoMeta } from "@hamur/adapter-react"
export default function Home() {
useServerSeoMeta({ googleSiteVerification: "abc123", ogType: "website" })
return <h1>Home</h1>
}
- The tags stay in the document after hydration and every navigation.
- They take no
titleortitleTemplate, since the browser would change the title. - A tag the browser sets for the same slot, such as the same meta
name, replaces the server's.
Position and priority
| Option | Values | Tags |
|---|---|---|
tagPosition | "head" (default), "bodyOpen" (first in <body>) or "bodyClose" (last) | link, script, style, noscript |
tagPriority | "critical", "high" or "low" | every tag |
Hamur orders <head> itself:
- charset, viewport,
base, the CSP and nonce meta tags and the import map; - the other tags and the title by priority:
critical,high, unset,low; - within one priority by kind:
http-equivmeta, title,preconnect, async scripts,@importstyles, classic scripts, stylesheets and styles,preloadandmodulepreload,deferand module scripts,prefetch, the rest; - then in the order written.
- The body of a script, style or noscript is
innerHTML; there is notextContent. - The tags of
app.headtake the CSP nonce or hash. So a tag manager is anapp.head.scriptand itsnoscripttwin withtagPosition: "bodyOpen". - Markup that is not a head tag goes in a server plugin's
render:htmlhook.
Structured data
Each object of jsonLd becomes one <script type="application/ld+json">. Objects with one @id keep one tag, the later. JsonLdThing has the rules.
Head from the route
head in a load file gets the load context and the data of load, and returns a head input. seo in it becomes meta tags.
- It runs on the server after the loads, and in the browser at hydration and after each navigation, with no request of its own.
- On static output it gets the data of the payload file.
- A
headthat throws is a load error of its route. - A page or layout file that exports
headfails the build (HAMUR_E1028).
meta.head of a page is for tags that need no data. A page with meta.ssr: false has them in its shell too.
import type { PageMeta } from "hamur/app"
export const meta = {
head: {
htmlAttrs: { lang: "tr" },
titleTemplate: (title) => (title ? `${title} | Mağaza` : "Mağaza"),
},
} satisfies PageMeta
Head components
HeadTitle, HeadMeta, HeadLink, HeadScript, HeadStyle, HtmlAttrs and BodyAttrs take props only, add one entry while they are mounted and render nothing.
Icons
File in app/ | Becomes | Served at |
|---|---|---|
favicon.ico | <link rel="icon" href sizes> | <app.baseURL>favicon.ico |
icon.*, icon<n>.* (ico, png, svg, jpg) | <link rel="icon" href type sizes> | icon-<hash>.<ext> under app.buildAssetsDir |
apple-icon.*, apple-icon<n>.* (png, jpg) | <link rel="apple-touch-icon" href sizes> | the same |
- The links are in this order, after the links of
app.head.jpegis an extension too. sizesis read from the file:"32x32", every entry of an ICO as"16x16 32x32","any"for an SVG.- A file that is not what its extension says fails the build, for example
app/icon.png: not a PNG file (no IHDR chunk). - Two favicons fail it with
public/favicon.ico and app/favicon.ico both exist; keep one. - Icons of a layer are not read.
Open Graph and Twitter images
opengraph-image and twitter-image (png, jpg, jpeg or gif) go in app/ or in a folder of app/pages/.
| File | Gives | Warns over |
|---|---|---|
opengraph-image.* | og:image, og:image:type, og:image:width, og:image:height | 8 MB |
opengraph-image.alt.txt | og:image:alt | |
twitter-image.* | twitter:image | 5 MB |
twitter-image.alt.txt | twitter:image:alt |
- A file applies to the pages in its folder and below, and to the page beside the folder:
blog.tsxforblog/opengraph-image.png. The nearest file wins,app/comes last. Group folders count. - The file is written as
<name>-<hash>.<ext>underapp.buildAssetsDir. Its URL is absolute throughsite.url; without it the build warns (HAMUR_W1221) and writes the path. - A page that sets
og:imageitself replaces the whole group. - A file that is not what its extension says fails the build with its path.
- A page file with one of these names is HAMUR_E1001. A generated image is a handler route whose URL the page sets with
useSeoMeta({ ogImage }), or OG images. - Files of a layer are not read, and an error page has no image.