Next.js · Middle · Question 14 of 75
How do you avoid a double DB query if both generateMetadata and the page call getUser(id)?
- ✓Correct answer: Wrap the function in React's
cache()- deduplication within one request - 2Store the user in a module-level global variable on the server
- 3Pass the data from
generateMetadatato the page via its props
Explanation
cache() memoizes calls with the same arguments for the duration of one server render. Next.js deduplicates fetch with identical parameters itself, but ORMs and SDKs need cache(). A global variable would mix different users' data.