# Page metadata

> Titles, descriptions and Open Graph tags, exported from the page.

A page sets its title and meta tags by exporting `metadata`. React moves the
tags into `<head>`, so there is no document template to edit:

```tsx title="src/app/about/page.tsx"
import type { Metadata } from '@rsc-kit/core/metadata';

export const metadata: Metadata = {
  title: 'About us',
  description: 'Learn more about our team.',
};

export default function AboutPage() {
  return <h1>About us</h1>;
}
```

## Static metadata

`metadata` takes the keys in [the table below](#supported-keys):

```tsx title="src/app/about/page.tsx"
export const metadata: Metadata = {
  title: 'About us',
  description: 'Learn more about our team.',
  keywords: ['about', 'team', 'company'],
  openGraph: {
    title: 'About us',
    description: 'Learn more about our team.',
  },
};
```

Import `Metadata` from `@rsc-kit/core/metadata`. The import resolves as soon as
dependencies are installed, before any build.

### Custom meta tags

A tag without a named key goes under `other`:

```tsx
export const metadata: Metadata = {
  title: 'Pricing',
  other: { 'fb:app_id': '1234', 'theme-color': '#111' },
};
```

The named keys are a closed set, so a typo such as `titel` fails the
typecheck.

## Metadata from data

When the values depend on what the page loads, export `generateMetadata`. It
receives the same `params` and `searchParams` a page does:

```tsx title="src/app/posts/[slug]/page.tsx"
import type { GenerateMetadata } from '@rsc-kit/core/metadata';
import { findPost } from '../../../data';

export const generateMetadata: GenerateMetadata<{ slug: string }> = async ({ params }) => {
  const { slug } = await params;
  const post = await findPost(slug);

  return {
    title: post.title,
    description: post.excerpt,
    openGraph: { title: post.title, description: post.excerpt, images: post.cover },
  };
};
```

It never holds the page up. The page paints at once, and the `<title>` and meta
tags stream in when the lookup finishes. A route with only static `metadata`
has its tags in the first bytes.

A search engine or a link-preview scraper is answered once the whole page has
rendered, so it always reads the tags in `<head>`. See
[Errors](/guides/errors) for how one is recognised.

A layout can export `generateMetadata` too. Its answer applies to every page
beneath it that doesn't set its own: a category named in its layout is the
title of every page under it without one, and the template of the layout above
still applies.

## Title templates

A layout sets the shape of every title beneath it. `%s` is the page's own
title:

```tsx title="src/app/layout.tsx"
export const metadata: Metadata = {
  title: {
    template: '%s | Acme',
    default: 'Acme',
  },
};
```

```tsx title="src/app/settings/page.tsx"
export const metadata: Metadata = { title: 'Settings' };

// <title>Settings | Acme</title>
```

The nearest layout with a template wins, so a nested layout can override the
one above it. A page with no title gets the `default`.

Metadata always resolves against the whole layout chain, so a navigation that
renders only part of the page still gets the title a full load would.

## Supported keys

| Key | Becomes |
| --- | --- |
| `title` | `<title>`: a string on a page, a template on a layout |
| `description` | `<meta name="description">` |
| `keywords` | `<meta name="keywords">`. `'react, rsc'` and `['react', 'rsc']` give the same tag |
| `author` | `<meta name="author">` |
| `robots` | `<meta name="robots">`: a string, or `{ index: false, follow: false }` for `noindex, nofollow`. `googleBot` is `<meta name="googlebot">` |
| `metadataBase` | nothing itself; it makes every relative URL below absolute |
| `alternates` | `<link rel="canonical">` from `canonical`, and `<link rel="alternate" hreflang>` per entry of `languages` |
| `openGraph` | `<meta property="og:…">`, one per field; each image brings its size and alt |
| `twitter` | `<meta name="twitter:…">` |
| `icons` | `<link rel="icon">`, `apple-touch-icon` and so on |
| `appleWebApp` | the iPhone home-screen tags |
| `other` | `<meta name="…">`, or `property=` for an `og:`, `fb:` or `article:` key |
| anything else | `<meta name="…">` |

The flat spellings, `'og:title'` and `'twitter:card'` at the top level, were
removed: a page that still has one fails with an error that names the key. Write
them as `openGraph` and `twitter`, which are typed and take an image with a
width and height. A tag those objects have no field for goes under `other`.

## Charset and viewport

Every document gets `<meta charSet="utf-8">` and
`<meta name="viewport" content="width=device-width, initial-scale=1">` without
being asked. A layout that renders either tag itself is left alone.

To change the viewport, export `viewport` from a layout or a page:

```tsx
import type { Viewport } from '@rsc-kit/core/metadata'

export const viewport: Viewport = {
  width: 'device-width',
  initialScale: 1,
  maximumScale: 1,
  userScalable: false,
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: '#ffffff' },
    { media: '(prefers-color-scheme: dark)', color: '#000000' },
  ],
  colorScheme: 'dark light',
}
```

Layouts merge from outer to inner, then the page, key by key. `themeColor` and
`colorScheme` become their own meta tags, and a `themeColor` here wins over the
web manifest's.

## The share card

Set the site-wide parts once, on the root layout:

```tsx title="src/app/layout.tsx"
export const metadata: Metadata = {
  metadataBase: new URL('https://example.com'),
  openGraph: {
    siteName: 'Example',
    type: 'website',
  },
  twitter: {
    card: 'summary_large_image',
    site: '@example',
  },
};
```

and each page adds its own:

```tsx title="src/app/posts/[slug]/page.tsx"
export async function generateMetadata({ params }): Promise<Metadata> {
  const post = await findPost((await params).slug);

  return {
    title: post.title,
    openGraph: {
      title: post.title,
      description: post.excerpt,
      url: `/posts/${post.slug}`,
      images: [{ url: post.cover, width: 1200, height: 630, alt: post.title }],
    },
  };
}
```

`openGraph` is replaced whole by a nearer segment's, as in Next, so a page that
sets its own `openGraph` should repeat the `siteName` it wants.

:::caution[`metadataBase` is not optional in production]
Facebook, Slack and LinkedIn ignore a relative image URL without saying so, and
the link unfurls with no image. Set `metadataBase` on the root layout and every
relative `url`, image and icon below it is made absolute. Without it, an
`opengraph-image.png` in `app/` goes out as `/_app/opengraph-image.png`, which
works in a browser and nowhere else.
:::

`og:` tags render with `property=` and `twitter:` tags with `name=`, because
that is what each scraper reads.

## Canonical and translated URLs

`alternates` names the URL search engines should index, and the same page in
each other language:

```tsx title="src/app/[locale]/about/page.tsx"
export const metadata: Metadata = {
  alternates: {
    canonical: '/en/about',
    languages: { en: '/en/about', fr: '/fr/about', 'x-default': '/en/about' },
  },
};
```

```html
<link rel="canonical" href="https://example.com/en/about">
<link rel="alternate" hreflang="en" href="https://example.com/en/about">
<link rel="alternate" hreflang="fr" href="https://example.com/fr/about">
<link rel="alternate" hreflang="x-default" href="https://example.com/en/about">
```

Relative URLs are made absolute with `metadataBase`. Like `openGraph`, it is
replaced whole by a nearer segment's. A site in several languages usually
writes it in `generateMetadata`, from the locale; see
[Internationalisation](/guides/internationalisation#telling-search-engines).

## On an iPhone's home screen

`appleWebApp` takes `capable`, `title`, `statusBarStyle` and `startupImage`:

```tsx title="src/app/layout.tsx"
export const metadata: Metadata = {
  appleWebApp: { capable: true, title: 'Orders', statusBarStyle: 'black-translucent' },
}
```

Launch screens are simpler as files named by their size. See
[Splash screens](/guides/pwa#splash-screens).

## Icons

`icons` takes a string, a list, or an object by role:

```tsx
// One favicon
export const metadata: Metadata = { icons: '/favicon.ico' };

// Several, with attributes
export const metadata: Metadata = {
  icons: [
    { url: '/favicon.ico', sizes: '32x32' },
    { url: '/icon-192.png', sizes: '192x192', type: 'image/png' },
  ],
};

// By role
export const metadata: Metadata = {
  icons: {
    icon: '/favicon.ico',
    apple: '/apple-touch-icon.png',
    other: { rel: 'mask-icon', url: '/safari-pinned-tab.svg', color: '#5bbad5' },
  },
};
```

Each one takes `url`, `type`, `sizes`, `color`, `rel`, `media` and
`fetchPriority`.

## Icons and share images

Usually you don't need `icons` at all. Put the files in `src/app/` and the
build finds them:

| File | Becomes |
| --- | --- |
| `favicon.ico` | served at `/favicon.ico` |
| `icon.png`, `icon-192.png`, `icon-512.png` | `<link rel="icon">`, and the [manifest](/guides/offline/)'s icons |
| `apple-icon.png` | `<link rel="apple-touch-icon">` |
| `opengraph-image.png` | `<meta property="og:image">` |
| `twitter-image.png` | `<meta name="twitter:image">`, with the large-card type |

The names are Next's, so an app moving between the two keeps its files.

Sizes are read from the filename: `icon-192.png` is 192 pixels. So a manifest
usually lists no icons:

```ts title="src/app/manifest.ts"
export default {
  name: 'Orders',
  themeColor: '#0b0b0c',
  // icons omitted: the build uses the icon-*.png beside this file
} satisfies WebManifest
```

They are served under `/_app/`, so they never collide with your `public/`
files. The favicon is the exception, served at `/favicon.ico` because browsers
ask for that URL whatever the markup says.

A `public/favicon.ico` is answered before the request reaches the app, so it
hides yours. Laravel's skeleton ships an empty one, which Herd answers with a
404. Dev start and the build warn when `public/` holds a file at a URL the app
serves, and name both; delete the one in `public/`.

### A page's own share image

Put an `opengraph-image.png` or `twitter-image.png` in any folder under `app/`
and it is the card for the pages in that folder and below. The nearest one
wins:

```text
src/app/
  opengraph-image.png            every page without a nearer one
  pricing/
    opengraph-image.png          /pricing
  (marketing)/
    laravel/
      opengraph-image.png        /laravel, the group never in the URL
  blog/[slug]/
    opengraph-image.png          every post
```

Each kind is looked up on its own, so a `twitter-image.png` in `pricing/` does
not bring an `og:image` with it.

A page can name its image in metadata instead, and it replaces the site's
rather than adding a second tag:

```tsx title="src/app/pricing/page.tsx"
export const metadata: Metadata = {
  openGraph: { images: [{ url: '/pricing-card.png', width: 1200, height: 630 }] },
}
```

The nearer declaration wins, whichever kind it is. An image exported by a page
beats a file in a folder above it, and a file in `pricing/` beats one a layout
exports from `app/`. In the same folder the file wins, as in Next. An
`openGraph` that names no images leaves the card to the file.

- Folders outside the URL tree are not searched: `_private`, `@slot` and
  `(.)intercepted`.
- Per-folder placement is not yet read for `icon` or `apple-icon`; those are
  the site's.
- The dev server restarts on its own to pick up a new share image.

### A different image per record

`opengraph-image.tsx`, a component Next renders to a PNG at build time, is not
supported. Make the image once and commit the file. For an image per record,
such as one per post, write an [API route](/guides/api-routes/) that returns
it, and point `openGraph.images` at its URL.

## How metadata reaches the page

- **Load:** React moves `<title>` and `<meta>` into `<head>` as they render.
- **Navigation:** the resolved metadata comes back in the `X-RSC-Meta` header,
  and the browser updates `document.title` and the tags in place.
