
Understanding Metadata in Next.js
Learn how to leverage static and dynamic metadata in Next.js to enhance SEO and social media sharing.
Next.js offers powerful tools for handling metadata, essential for SEO and enhancing how your content appears on social media. This guide will cover static and dynamic metadata, including Open Graph and Twitter metadata, to ensure your site looks great and performs well.
Static Metadata#
Static metadata is defined directly in your component files. It's perfect for content that doesn't change often, like static pages. Here's an example of how to set up static metadata in a layout.js or page.js file:
import type { Metadata } from 'next';
export const metadata: Metadata = {
title: 'My Awesome Blog',
description: 'A blog about web development and more.',
};
export default function Page() {
return <div>Welcome to my blog!</div>;
}This static metadata will automatically generate the relevant <meta> tags in the HTML head, improving your site's SEO.
Dynamic Metadata with generateMetadata#
For pages where metadata needs to change dynamically based on the content, use the generateMetadata function. This is especially useful for pages that fetch data at runtime, such as blog posts or product pages.
Here's an example using generateMetadata:
import type { Metadata, ResolvingMetadata } from 'next';
type Props = {
params: { id: string };
searchParams: { [key: string]: string | string[] | undefined };
};export async function generateMetadata( { params }: Props,
parent?: ResolvingMetadata ): Promise<Metadata> {
const product = await fetch(`https://api.example.com/products/${params.id}`).then(res => res.json());
return {
title: product.name,
description: product.description,
openGraph: {
title: product.name,
description: product.description,
images: [product.image],
},
twitter: {
card: 'summary_large_image',
title: product.name,
description: product.description,
image: product.image,
},
};
}This approach ensures your metadata is accurate and up-to-date with your content, enhancing SEO and social media sharing.
Open Graph and Twitter Metadata#
Open Graph and Twitter metadata are crucial for controlling how your content appears when shared on social media platforms.
Open Graph Metadata#
Open Graph metadata improves the appearance of links shared on platforms like Facebook and LinkedIn. Here's how to set it up:
export const metadata: Metadata = {
openGraph: {
title: 'My Awesome Blog',
description: 'A blog about web development and more.',
url: 'https://example.com/blog',
type: 'website',
images: [
{
url: 'https://example.com/images/og-image.jpg',
width: 800,
height: 600,
alt: 'Og Image Alt',
},
],
},
};Twitter Metadata#
Twitter metadata, or Twitter Cards, ensure your content looks great when shared on Twitter. Here's an example:
export const metadata: Metadata = {
twitter: {
card: 'summary_large_image',
site: '@site_username',
title: 'My Awesome Blog',
description: 'A blog about web development and more.',
image: 'https://example.com/images/twitter-card.jpg',
},
};Conclusion#
Properly managing metadata in Next.js is crucial for SEO and enhancing how your content is displayed on social media. By using both static and dynamic metadata, along with Open Graph and Twitter metadata, you can ensure your site performs well and looks great when shared.
For more detailed information, you can refer to the Next.js Metadata API documentation.

