#dynamic metadata
1 messages · Page 1 of 1 (latest)
To pass the title argument from an exported function to the built-in metadata in Next.js, you can use the generateMetadata function. This function allows you to dynamically set metadata based on route parameters or other dynamic data.
Here's how you can achieve this:
-
Define the
generateMetadatafunction: This function will take thetitleargument and return a metadata object with thetitleset dynamically. -
Use the
generateMetadatafunction in your component: You can then use this function in your component to set the metadata dynamically.
Here is an example based on the provided code:
import { Metadata } from 'next'
type Props = {
params: { title: string }
}
export async function generateMetadata({ params }: Props): Promise<Metadata> {
return {
title: params.title,
}
}
export default function HeadContent({ title }: { title: string }) {
// Your component logic here
return (
<>
<h1>{title}</h1>
</>
)
}
In this example:
- The
generateMetadatafunction takesparamsas an argument, which includes thetitle. - It returns a metadata object with the
titleset toparams.title. - The
HeadContentcomponent can then use this title as needed.
For more details on using generateMetadata, you can refer to the Next.js documentation on dynamic metadata (https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function).