Reuse data fetched in page inside generateMetadata #50080
Replies: 12 comments 13 replies
-
I have a same question :D Waiting... |
Beta Was this translation helpful? Give feedback.
-
You can use the import { cache } from "react";
export const getPost = cache(() => {
console.log("called getPost()");
return "post";
}); import { Metadata } from "next";
export function generateMetadata(): Metadata {
const posts = getPost();
return {
title: "title",
};
}
const Page = async () => {
const posts = getPost();
return <article>{"article"}</article>;
};
export default Page; "called getPost()" is output only once. ref. https://nextjs.org/docs/app/building-your-application/data-fetching/caching#react-cache |
Beta Was this translation helpful? Give feedback.
-
Fetch is deduplicated automatically, so you don't need to use There is no way to share the data between the server component and |
Beta Was this translation helpful? Give feedback.
-
why is this common use case not in documentation |
Beta Was this translation helpful? Give feedback.
-
Note that React cache won't help anything when using async functions for data retrieval, since the generateMetadata and Page functions are executed at the same time (so both will start do the same async thing and not "wait" for each other, causing duplicate data retrieval). Is there any known solution to share async loaded data between the page and metadata, or set the metadata inside the page function? |
Beta Was this translation helpful? Give feedback.
-
Still not clear why data cannot be reused or passed between the page component and its generateMetadata ... |
Beta Was this translation helpful? Give feedback.
-
Even I am facing the same problem. Data fetch twice on every request is going to cost us twice. There should be some way to mitigate this issue. |
Beta Was this translation helpful? Give feedback.
-
Commenting to stay tuned :) |
Beta Was this translation helpful? Give feedback.
-
Following because |
Beta Was this translation helpful? Give feedback.
-
I'm really not getting these "just use cache bro, nextJS fetch does it for you" responses... Do people think that the only form of async data retrieval in existence on the entire planet is via Can we just admit that caching isn't a valid solution for all use cases, and this is inevitably going to be a common workflow, so can we just agree that a better solution should be implemented by |
Beta Was this translation helpful? Give feedback.
-
interested in getting an answer to this too |
Beta Was this translation helpful? Give feedback.
-
I am using nextjs15. Is it possible to reuse the data retrieved by calling API in generateMetadata() in the same way in page.tsx? |
Beta Was this translation helpful? Give feedback.
-
I have fetched data in the page server component. How can I use the same data in the generateMetadata function without fetching it again?
Beta Was this translation helpful? Give feedback.
All reactions