# Head details

The rules behind the [Head](/head) guide. [`HeadInput`](/api-reference/app#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](/seo))                                                              |
| 2     | `app.head` of the config                                                                     |
| 3     | the [icon](#icons) and [image](#open-graph-and-twitter-images) files                         |
| 4     | entries an app plugin adds                                                                   |
| 5     | the [head of each matched route](#head-from-the-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.

````ts [head-config.ts]
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 `useHead` the template may be a function. It gets `undefined` when 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](#template-params).

````tsx [react/title-template.tsx]
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                                                                 |

````tsx [react/head-order.tsx]
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:*`.

```ts
useHead({
  meta: [
    { property: "og:image", content: "/a.png" },
    { property: "og:image", content: "/b.png" },
  ],
})
```

- `og:image:width` and the like belong to the `og:image` before them. The same holds for `og:video`, `og:audio` and `twitter:image`.
- Another entry that sets `og:image` replaces the whole group, with its widths and alt texts.
- One without its root in the same entry is dropped ([HAMUR_W3005](/errors/HAMUR_W3005)).
- A tag with its own `key` is never part of a group.

## Search and social tags

[`SeoMetaInput`](/api-reference/app#seometainput-ogimage) lists the keys of `useSeoMeta` and the tags they write.

## Safe input

[`useHeadSafe`](/api-reference/adapters#useheadsafe) lists what it keeps and what it drops. `hamur dev` logs each drop once, without the value ([HAMUR_W3006](/errors/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.

````tsx [react/head-server.tsx]
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 `title` or `titleTemplate`, 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:

1. charset, viewport, `base`, the CSP and nonce meta tags and the import map;
2. the other tags and the title by priority: `critical`, `high`, unset, `low`;
3. within one priority by kind: `http-equiv` meta, title, `preconnect`, async scripts, `@import` styles, classic scripts, stylesheets and styles, `preload` and `modulepreload`, `defer` and module scripts, `prefetch`, the rest;
4. then in the order written.

- The body of a script, style or noscript is `innerHTML`; there is no `textContent`.
- The tags of `app.head` take the CSP nonce or hash. So a tag manager is an `app.head.script` and its `noscript` twin with `tagPosition: "bodyOpen"`.
- Markup that is not a head tag goes in a server plugin's `render:html` hook.

## Structured data

Each object of `jsonLd` becomes one `<script type="application/ld+json">`. Objects with one `@id` keep one tag, the later. [`JsonLdThing`](/api-reference/app#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 `head` that throws is a load error of its route.
- A page or layout file that exports `head` fails the build ([HAMUR_E1028](/errors/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.

````ts [seo/page-meta.ts]
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`](/api-reference/adapters#headtitle-headmeta-headlink-headscript-headstyle-htmlattrs-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`. `jpeg` is an extension too.
- `sizes` is 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.tsx` for `blog/opengraph-image.png`. The nearest file wins, `app/` comes last. Group folders count.
- The file is written as `<name>-<hash>.<ext>` under `app.buildAssetsDir`. Its URL is absolute through `site.url`; without it the build warns ([HAMUR_W1221](/errors/HAMUR_W1221)) and writes the path.
- A page that sets `og:image` itself 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](/errors/HAMUR_E1001). A generated image is a handler route whose URL the page sets with `useSeoMeta({ ogImage })`, or [OG images](/og-image).
- Files of a layer are not read, and an error page has no image.
