-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Clarify React.cache()
usage in generateMetadata()
#62162
Comments
Hello @kevinmitch14 👋🏼 I haven't been able to reproduce this with
While the React docs talk about it in being in components, I believe in Next.js, Are you able to provide a minimum reproduction? |
Have been playing around with this, not sure if I remember the exact issue I was encountering but here's a repro - https://github.com/kevinmitch14/nextjs-metadata-cache I've narrowed it down to the presence of With Without Root layout: export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
unstable_noStore();
return (
<html lang="en">
<body className={inter.className}>
<div className="flex">
<div className="bg-red-200 w-60 flex flex-col h-screen">
<Link href={"/blog"}>Blog</Link>
<Link href={"/news"}>News</Link>
</div>
{children}
</div>
</body>
</html>
);
} |
It also seems that the Steps to reproduce:
I've raised this in #62165 also. Even when the |
Doing a bit more research, this is due to streaming?
In order to show So when Nextjs prefetches the nearest This is all making sense to me but it still confuses me why |
Thank you for the reproduction! Looking into the multiple calls when
I'd expect this to happen, consider a title template, we'd need to call the parent |
Thanks for the response. Hmm, yeah. My impression was that anything in
Can the template pattern be achieved by having a base metadata in the layout.tsx and then overwrite this in page.tsx for example? Using export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
// fetch data
const product = await fetch(`https://.../${id}`).then((res) => res.json())
// optionally access and extend (rather than replace) parent metadata
const previousImages = (await parent).openGraph?.images || []
return {
title: product.title,
openGraph: {
images: ['/some-specific-page-image.jpg', ...previousImages],
},
}
} For context, we have a multi-tenant application, and would like to show the tenants logo as the favicon/icon. Hitting the DB to get this on every navigation is not very realistic. I am not using fetch either. I guess we can use something like |
What is the improvement or update you wish to see?
This part of the docs, gives the impression that
cache()
can be used in Metadata.Is there any context that might help us understand?
From Next docs:
However in the docs on www.react.dev, it states that
cache()
can only be used in components - here.I'm not 100% on the Metadata API and how it works with
React.cache()
but I've been debugging an issue in relation to this and it seems that, indeed, cache is not applied in Metadata. Whereas, when usingfetch()
caching/deduping is applied.It would be great to get clarification on the expected behaviour here. Happy to create a PR for updating docs but would like to understand more before doing so.
Does the docs page already exist? Please link to it.
https://nextjs.org/docs/app/building-your-application/optimizing/metadata
The text was updated successfully, but these errors were encountered: