September 2, 2024

Md. Saad

Metadata is data that provides information about other data. It describes the data's characteristics, context, quality, condition, or other descriptive aspects. On a webpage, these data are not directly shown to the users. Rather it can help search engines, browsers, and other technologies.
Dynamic metadata changes or updates automatically based on a certain page. Unlike static metadata, which remains constant, dynamic metadata can change for each page. For instance, if there are several blog pages, the metadata for each page will be unique.
To create dynamic metadata in a Next.js application using the App Router, we can use Next.js's metadata API to generate metadata based on the page's content dynamically. This is particularly useful for setting dynamic <title>, <meta> tags, and other SEO-related tags that change depending on the page's content.
Here's how we can create dynamic metadata with Next.js App Router:
// app/posts/[id]/page.js
import { getPostData } from '@/lib/posts';
// Example to fetch data statically or dynamically
export async function generateMetadata({ params, searchParams }, parent) {
const { id } = params;
const post = await getPostData(id);
return {
title: post.title,
description: post.excerpt,
};
}
export default function Post({ params }) {
const { id } = params;
const post = await getPostData(id); // Assume this function fetches post data
return (
<main>
<h1>{post.title}</h1>
<p>{post.content}</p>
</main>
);
}In this example:
generateMetadata function accepts several props. props is an object containing the parameters of the current route:
generateMetadata should return a Metadata object containing one or more metadata. A list of Basic Metadata fields is given below:
export const metadata = {
generator: 'Next.js',
applicationName: 'Next.js',
referrer: 'origin-when-cross-origin',
keywords: ['Next.js', 'React', 'JavaScript'],
authors: [{ name: 'Seb' }, { name: 'Josh', url: 'https://nextjs.org' }],
creator: 'Jiachi Liu',
publisher: 'Sebastian Markbåge',
formatDetection: {
email: false,
address: false,
telephone: false,
},
}We can also define static metadata at the layout level if the metadata doesn't need to be dynamic for every page. This is done by exporting a metadata object in a layout file:
// app/layout.js
export const metadata = {
title: 'My Blog',
description: 'Welcome to my blog where I share my thoughts and tutorials.',
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}Note: We can use both static and dynamic metadata together. The static metadata in layout.js provides a default for all pages, and the dynamic metadata in page.js overrides it for specific routes as needed.
Using dynamic metadata in Next.js with the App Router is straightforward and helps improve our application's SEO and social sharing capabilities. By utilizing the generateMetadata function and the metadata object, we can dynamically set metadata based on the content or context of the page, ensuring our web pages are properly indexed and displayed in search engine results and social media.
If you still feel the need for help, feel free to contact us. The dedicated teams at StaticMania are ready to assist you 24/7.