Meta description: Learn how to configure metadata in Next.js App Router using static metadata, dynamic metadata, Open Graph images, canonical URLs, and SEO best practices.
Metadata helps search engines understand your pages and controls how your website appears in search results and social media previews.
Use static metadata when a page’s title and description do not depend on dynamic data.
import type { Metadata } from "next";export const metadata: Metadata = {title: "Plant Care Blog",description:"Discover tips and guides for growing healthy plants.",};export default function BlogPage() {return <h1>Plant Care Blog</h1>;}
Next.js generates HTML metadata similar to:
<title>Plant Care Blog</title><metaname="description"content="Discover tips and guides for growing healthy plants."/>
You can define common metadata in app/layout.tsx so your pages inherit a consistent configuration.
import type { Metadata } from "next";export const metadata: Metadata = {metadataBase: new URL("https://greengarden.com"),title: {default: "Green Garden",template: "%s | Green Garden",},description:"Discover beautiful plants and learn how to care for them.",openGraph: {siteName: "Green Garden",locale: "en_US",type: "website",},robots: {index: true,follow: true,},};
Let’s look at the important properties:
metadataBase: Defines the base URL for resolving relative metadata URLs.title.default: Sets the default title when a more specific title isn’t provided.title.template: Adds a consistent suffix to child page titles.openGraph: Defines default information for social media previews.robots: Controls whether search engines should index pages and follow links.Static metadata isn’t sufficient for pages whose content changes based on a URL, such as individual blog posts or product pages.
Use generateMetadata() to generate metadata from dynamic data. Consider the following route:
app/└── blog/└── [slug]/└── page.tsx
import type { Metadata } from "next";import { notFound } from "next/navigation";async function getPost(slug: string) {return posts[slug as PostSlug] ?? null;}export async function generateMetadata({params,}: Props): Promise<Metadata> {const { slug } = await params;const post = await getPost(slug);if (!post) {return {title: "Post Not Found",robots: {index: false,follow: false,},};}return {title: post.title,description: post.description,};}
Open Graph metadata controls how a page appears when someone shares it on supported social platforms.
You can configure an image, title, description, and page type.
import type { Metadata } from "next";export const metadata: Metadata = {title: "Monstera Care Guide",description: "Learn how to grow and care for Monstera plants.",openGraph: {title: "Monstera Care Guide",description:"A practical guide to growing healthy Monstera plants.",type: "article",images: [{url: "/images/monstera-og.jpg",width: 1200,height: 630,alt: "Monstera plant care guide",},],},twitter: {card: "summary_large_image",title: "Monstera Care Guide",description:"A practical guide to growing healthy Monstera plants.",images: ["/images/monstera-og.jpg"],},};
Because metadataBase is configured in the root layout, Next.js can resolve the relative image URL to an absolute URL.
For example:
https://greengarden.com/images/monstera-og.jpg
For best results, use an appropriate, publicly accessible image and verify how the preview appears on your target platforms.