Next.js Discord

Discord Forum

Dynamic title for meta tag

Answered
Caya posted this in #help-forum
Open in Discord
Avatar
Here is my file structure
app/
|-(userProfile)
    |-[username]
        |-page.tsx
    |-layout.tsx


now i want to generate a dynamic title for the page like if its username and it exist then page title will be "username's page" else it will show default page so can we generate dynamic meta tag in layout tsx and sld i do it or its a bad practice
Answered by Pearls
Im pretty sure that this is not the correct way to do it but you could simply add a <title> element in your page.

<title>{username}</title>
View full answer

2 Replies

Avatar
Im pretty sure that this is not the correct way to do it but you could simply add a <title> element in your page.

<title>{username}</title>
Answer
Avatar
You can export generateMetadata from your page like so:

export async function generateMetadata({ params }: { params: ServerParams }): Promise<Metadata> {
  // do your thing
}


Generate Metadata doc: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function