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.
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.
_10// app/layout.tsx_10import type { Metadata } from 'next';_10_10export const metadata: Metadata = {_10 metadataBase: new URL('https://example.com'),_10 title: { default: 'Acme', template: '%s | Acme' },_10 description: 'Guides and tools for building with Acme.',_10};
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.
_11// app/pricing/page.tsx_11import type { Metadata } from 'next';_11_11export const metadata: Metadata = {_11 title: 'Pricing', // renders as "Pricing | Acme"_11 description: 'Plans for teams of every size.',_11};_11_11export default function Page() {_11 return <h1>Pricing</h1>;_11}
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.
_22// app/blog/[slug]/page.tsx_22import type { Metadata } from 'next';_22import { getPost } from '@/lib/posts';_22_22type Props = { params: Promise<{ slug: string }> };_22_22export async function generateMetadata({ params }: Props): Promise<Metadata> {_22 const { slug } = await params;_22 const post = await getPost(slug);_22_22 return {_22 title: post.title,_22 description: post.excerpt,_22 openGraph: { title: post.title, description: post.excerpt, type: 'article' },_22 };_22}_22_22export default async function Page({ params }: Props) {_22 const { slug } = await params;_22 const post = await getPost(slug);_22 return <article><h1>{post.title}</h1></article>;_22}
The page and the metadata both call getPost. Wrap that function in React's cache so the data is only fetched once per request.
_10// lib/posts.ts_10import { cache } from 'react';_10_10export const getPost = cache(async (slug: string) => {_10 return db.post.findUnique({ where: { slug } });_10});
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.
_21// app/blog/[slug]/opengraph-image.tsx_21import { ImageResponse } from 'next/og';_21import { getPost } from '@/lib/posts';_21_21export const size = { width: 1200, height: 630 };_21export const contentType = 'image/png';_21export const alt = 'Blog post cover';_21_21export default async function Image({ params }: { params: Promise<{ slug: string }> }) {_21 const { slug } = await params;_21 const post = await getPost(slug);_21_21 return new ImageResponse(_21 (_21 <div style={{ display: 'flex', width: '100%', height: '100%', alignItems: 'center', justifyContent: 'center', fontSize: 64, background: 'white' }}>_21 {post.title}_21 </div>_21 ),_21 size_21 );_21}
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
openGraphfields you still need in the child, or share them from a constant. - Relative image URLs without metadataBase. Set
metadataBasein the root layout so links resolve to your real domain. - Both a file and a config image. An
opengraph-imagefile takes priority overopenGraph.imagesin 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
defaultfor that level. - Synchronous params. Code that reads
params.slugwithoutawaitfails in Next.js 16.
Related reading
#Was this resource helpful?