How to use dynamic metadata in Next.js
You must be signed in to create a new issue

closed

was closed 250 days ago

Issue Details

Stuck with static titles! Need dynamic metadata in Next.js.

I tried adding a title, but it's not changing based on my content. How can I make my title, description, and other metadata dynamic to improve SEO and user experience?
Want custom titles and descriptions for my blog posts in Next.js. How can I set dynamic metadata based on each post?

Solutions

Submitted by Tajwar Saiyeed

Here is a beautiful solution


In you next.js page component add the this code

export const generateMetadata = async ({params: {postId}}: IParams) => {
  const {post} = await getPost(postId);
  
  return {
    title: post.title,
    description: post.description
    };
}

Official Documentation

Submitted 251 days ago
T