Question presented to candidate: "Three components on the same page each call fetch for the current user. How many network requests happen, and how would you get the same behaviour for a database query?"
What a strong answer should cover:
- Next.js memoizes
fetchGET requests with the same URL and options for the duration of one server render. - It applies across layouts, pages, Server Components,
generateMetadataandgenerateStaticParams, but not in Route Handlers. - It is independent of the Data Cache: even
no-storerequests are deduplicated within a render. - For non-fetch work (ORM, SDK) wrap the function in React
cache()to get the same per-request deduplication. - It resets for every request, so it is safe for per-user data.
Clarifying questions expected:
- "Is it a fetch or a database call?" β only fetch is memoized automatically.
- "Do the calls use exactly the same URL and options?" β any difference is a separate request.
Code / implementation expected: Yes β a React.cache-wrapped data function used in several components.