文档:元数据
1、静态数据
import type { Metadata } from 'next'export const metadata: Metadata = {title: '...',description: '...',
}export default function Page() {}
2、动态数据
import type { Metadata, ResolvingMetadata } from 'next'type Props = {params: { id: string }searchParams: { [key: string]: string | string[] | undefined }
}export async function generateMetadata({ params, searchParams }: Props,parent: ResolvingMetadata
): Promise<Metadata> {// read route paramsconst id = params.id// fetch dataconst product = await fetch(`https://.../${id}`).then((res) => res.json())// optionally access and extend (rather than replace) parent metadataconst previousImages = (await parent).openGraph?.images || []return {title: product.title,openGraph: {images: ['/some-specific-page-image.jpg', ...previousImages],},}
}export default function Page({ params, searchParams }: Props) {}