Next.js Metadata API Explained — SEO and Social Cards
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you will learn about Next.js Metadata API Explained. We cover key concepts, practical examples, and best practices to help you master this topic.
Next.js Metadata API defines page metadata (title, description, Open Graph, Twitter cards) for SEO and social sharing, with support for static objects and dynamic generation functions.
What You'll Learn
- Static metadata export
- generateMetadata function
- Open Graph and Twitter cards
- JSON-LD structured data
- Sitemap and robots.txt
Why It Matters
Metadata drives SEO rankings and social sharing appearance. Next.js metadata is automatically added to the HTML head with proper tags for search engines and social platforms.
// app/page.jsx — Static metadata
export const metadata = {
title: "My App",
description: "A Next.js application with great SEO",
openGraph: {
title: "My App",
description: "A Next.js application with great SEO",
type: "website",
images: [{ url: "/og-image.png", width: 1200, height: 630 }],
},
twitter: {
card: "summary_large_image",
title: "My App",
description: "A Next.js application with great SEO",
images: ["/og-image.png"],
},
};
// app/posts/[slug]/page.jsx — Dynamic metadata
export async function generateMetadata({ params, searchParams }, parent) {
const post = await fetch(`https://api.example.com/posts/${params.slug}`)
.then(r => r.json());
return {
title: post.title,
description: post.excerpt,
openGraph: {
title: post.title,
description: post.excerpt,
type: "article",
publishedTime: post.publishedAt,
authors: [post.author.name],
images: [{ url: post.image, width: 1200, height: 630 }],
},
alternates: {
canonical: `/posts/${post.slug}`,
},
};
}
Expected output: Each page has SEO-optimized metadata with Open Graph and Twitter cards for social sharing previews.
← Previous
Next.js Analytics Explained — Measuring User Interactions
Next →
Next.js Instrumentation Explained — Monitoring and Observability
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro