Improved generateMetadata() for transferring data directly to templates without re-accessing the server #66688
Hiwo-1101000-1101001
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
Background
Currently, in Next.js, generateMetadata allows for setting metadata dynamically, but if the same data also needs to be used in the template, developers have to perform the data fetching twice – once in generateMetadata and once in the component itself. This leads to redundant network requests and a less optimal performance. By enabling the ability to pass data fetched in generateMetadata directly to the rendering template, we can improve both performance and developer experience. Although developers can manually workaround this by creating custom middlewares or HOCs, these solutions aren't as clean or efficient.
As my Google searches show, I'm not the only one with this problem:
The only solution I have found is caching the request, but it seems to me that this is not the best solution to the problem
Proposal
The proposed feature will modify generateMetadata to allow returning a payload that can be directly passed to the rendering component, eliminating the need for redundant data fetching. This can be achieved by returning an additional key in the object that generateMetadata returns. For example:
In the above example, template Data would be directly available in the rendering component. Implementation could involve extending the generateMetadata function to handle this additional key and appropriately manage its flow to the template.
Are you interested in contributing?:
Yes, I am interested in contributing to the implementation and can assist with both coding and documentation.
Beta Was this translation helpful? Give feedback.
All reactions