Page params to generateMetadata
Answered
Japanese Bobtail posted this in #help-forum
Japanese BobtailOP
Hi there. Is it possible to get the result of "getPage" (server fetch) to the generateMetadata Function? Otherwise i've to call the api twice? Or doesn't matter because the fetch is caching?
export async function generateMetadata({ params }) {
const page = await getPage(params.slug);
if (!page) return false;
return {
title: page?.title '',
description: page?.excerpt '',
};
}
export default async function Page({ params }) {
const page = await getPage(params.slug);
if (!page) return notFound();
//...
}
Thanks for help.
export async function generateMetadata({ params }) {
const page = await getPage(params.slug);
if (!page) return false;
return {
title: page?.title '',
description: page?.excerpt '',
};
}
export default async function Page({ params }) {
const page = await getPage(params.slug);
if (!page) return notFound();
//...
}
Thanks for help.
Answered by Ray
it should be fine if
getPage is cached.fetch requests are automatically memoized for the same data across generateMetadata, generateStaticParams, Layouts, Pages, and Server Components. React cache can be used if fetch is unavailable.
4 Replies
@Japanese Bobtail Hi there. Is it possible to get the result of "getPage" (server fetch) to the generateMetadata Function? Otherwise i've to call the api twice? Or doesn't matter because the fetch is caching?
export async function generateMetadata({ params }) {
const page = await getPage(params.slug);
if (!page) return false;
return {
title: page?.title || '',
description: page?.excerpt || '',
};
}
export default async function Page({ params }) {
const page = await getPage(params.slug);
if (!page) return notFound();
//...
}
Thanks for help.
it should be fine if
getPage is cached.fetch requests are automatically memoized for the same data across generateMetadata, generateStaticParams, Layouts, Pages, and Server Components. React cache can be used if fetch is unavailable.
Answer
@Japanese Bobtail Thanks for help sir.
btw, I think its better to call
notFound() in generateMetadata instead of return falseif (!page) return notFound();@Ray btw, I think its better to call `notFound()` in `generateMetadata` instead of return false
ts
if (!page) return notFound();
Japanese BobtailOP
I will do that