Learn how to configure SEO metadata for your application.
You can define static metadata in your layout.tsx or page.tsx files using the metadata export:
// app/layout.tsx
export const metadata = {
title: 'My Website',
description: 'Welcome to my website',
keywords: ['Next.js', 'React', 'JavaScript'],
openGraph: {
title: 'My Website',
description: 'Welcome to my website',
url: 'https://mywebsite.com',
siteName: 'My Website',
images: [
{
url: 'https://mywebsite.com/og.png',
width: 1200,
height: 630,
},
],
locale: 'en-US',
type: 'website',
},
}
For dynamic routes, use generateMetadata function to generate metadata based on route parameters:
// app/blog/[slug]/page.tsx
export async function generateMetadata({ params }) {
const post = await getPost(params.slug)
return {
title: post.title,
description: post.excerpt,
openGraph: {
title: post.title,
description: post.excerpt,
images: [post.ogImage],
},
}
}
Next.js supports special metadata files that are automatically handled:
favicon.ico
, apple-icon.jpg
, icon.jpg
- Place in app/ directoryopengraph-image.jpg
- For Open Graph imagesrobots.txt
- For search engine crawling rulessitemap.xml
- For search engine indexingMetadata cascades through the route hierarchy:
You can add structured data using JSON-LD:
export default function Page() {
return (
<>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@context": "https://schema.org",
"@type": "Article",
"headline": "Article headline",
"author": {
"@type": "Person",
"name": "John Doe"
}
})
}}
/>
{/* Page content */}
</>
)
}