---
title: "How do I set the page title, description and Open Graph image in the Next.js App Router?"
description: "Export a metadata object or a generateMetadata function from a layout or page, and add an opengraph-image file. Here is how titles, descriptions and OG images fit together."
url: https://www.danny.engineering/answer/nextjs/how-do-i-set-the-page-title-description-and-open-graph-image-in-the-nextjs-app-router
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Next.js"]
---

# How do I set the page title, description and Open Graph image in the Next.js App Router?

## The short answer


In the App Router you do not write `<head>` tags yourself. Export a `metadata` object from a `layout.tsx` or `page.tsx` for fixed values, or an async `generateMetadata` function when the values depend on data. For the social preview image, drop an `opengraph-image.png` (or `opengraph-image.tsx`) file into the route folder.


```typescript
// app/layout.tsx
import type { Metadata } from 'next';

export const metadata: Metadata = {
  metadataBase: new URL('https://example.com'),
  title: { default: 'Acme', template: '%s | Acme' },
  description: 'Guides and tools for building with Acme.',
};
```


## Why it happens


In the Pages Router you used `next/head` and rendered tags inside your components. The App Router replaced that with the Metadata API. Next.js reads the `metadata` and `generateMetadata` exports from every layout and page on the way down to the current route, merges them, and writes the final tags into the document for you. This lets Next.js put metadata in the right place, avoid duplicates, and stream it for dynamic pages.


Two rules come with this. First, these exports only work in Server Components, so a file marked `'use client'` cannot export `metadata`. Second, merging is shallow: a child that sets `openGraph` replaces the parent's whole `openGraph` object rather than adding to it.


## How to fix it


### Static metadata with a title template


Set a default title and a template in the root layout. Each page then only supplies its own part of the title.


```typescript
// app/pricing/page.tsx
import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'Pricing', // renders as "Pricing | Acme"
  description: 'Plans for teams of every size.',
};

export default function Page() {
  return <h1>Pricing</h1>;
}
```


`metadataBase` in the root layout turns relative URLs, such as `/og.png`, into absolute ones. Social networks need absolute image URLs, so set it once at the top.


### generateMetadata for data-driven pages


When the title comes from a database or CMS, export `generateMetadata`. In Next.js 15 and 16, `params` is a Promise, so await it.


```typescript
// app/blog/[slug]/page.tsx
import type { Metadata } from 'next';
import { getPost } from '@/lib/posts';

type Props = { params: Promise<{ slug: string }> };

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;
  const post = await getPost(slug);

  return {
    title: post.title,
    description: post.excerpt,
    openGraph: { title: post.title, description: post.excerpt, type: 'article' },
  };
}

export default async function Page({ params }: Props) {
  const { slug } = await params;
  const post = await getPost(slug);
  return <article><h1>{post.title}</h1></article>;
}
```


The page and the metadata both call `getPost`. Wrap that function in React's `cache` so the data is only fetched once per request.


```typescript
// lib/posts.ts
import { cache } from 'react';

export const getPost = cache(async (slug: string) => {
  return db.post.findUnique({ where: { slug } });
});
```


### A static Open Graph image


Place an image named `opengraph-image.png` (or `.jpg`) in a route folder. Next.js adds the `og:image` tags, including width and height, for that route and every route below it. A more specific file deeper in the tree wins. You can add `twitter-image.png` the same way.


### A generated Open Graph image


For one image per post, create `opengraph-image.tsx` and return an `ImageResponse` from `next/og`. In Next.js 16, `params` here is also a Promise.


```typescript
// app/blog/[slug]/opengraph-image.tsx
import { ImageResponse } from 'next/og';
import { getPost } from '@/lib/posts';

export const size = { width: 1200, height: 630 };
export const contentType = 'image/png';
export const alt = 'Blog post cover';

export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPost(slug);

  return new ImageResponse(
    (
      <div style={{ display: 'flex', width: '100%', height: '100%', alignItems: 'center', justifyContent: 'center', fontSize: 64, background: 'white' }}>
        {post.title}
      </div>
    ),
    size
  );
}
```


`ImageResponse` supports flexbox and a subset of CSS. Grid layouts will not work.


## Common pitfalls

- **Exporting metadata from a client component.** Move `'use client'` code into a child component and keep the page a Server Component.
- **Losing parent Open Graph fields.** Because merging is shallow, repeat any `openGraph` fields you still need in the child, or share them from a constant.
- **Relative image URLs without metadataBase.** Set `metadataBase` in the root layout so links resolve to your real domain.
- **Both a file and a config image.** An `opengraph-image` file takes priority over `openGraph.images` in the metadata object. Pick one approach per route.
- **Using the template on the layout itself.** The template applies to child segments, not to the layout that defines it. Use `default` for that level.
- **Synchronous params.** Code that reads `params.slug` without `await` fails in Next.js 16.

## Related reading

- [Next.js: Metadata and OG images](https://nextjs.org/docs/app/getting-started/metadata-and-og-images)
- [Next.js: generateMetadata](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)
- [Next.js: opengraph-image](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image)
