n the Next.js App Router, how does data fetching using the native fetch API behave, particularly concerning its built-in Data Cache? #81245
-
SummaryExplain how you would manage data freshness for a frequently updated page section (e.g., a real-time analytics dashboard or a breaking news feed) using both the revalidate option within fetch and on-demand revalidation (revalidateTag). Discuss the specific scenarios where each approach is most suitable and their respective trade-offs regarding performance, data accuracy, and development complexity. Additional information.. ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
By default, fetch requests in Server Components are cached on the server (“Data Cache”). On the first request, Next.js fetches data from the remote source and stores it in the Data Cache. Subsequent requests for the same resource will return the cached data, as long as it’s still fresh. If the cache is stale or missing, Next.js fetches new data and updates the cache. You can control caching per request with the cache option: cache: 'force-cache' (default): Uses the Data Cache if possible. cache: 'no-store': Always fetches fresh data and does not cache the response. Revalidation: You can set a next.revalidate option to specify how often cached data should be considered stale and refetched. This enables Incremental Static Regeneration-like behavior for server components. Client-side Router Cache: Next.js also maintains a client-side Router Cache that stores the React Server Component payloads for route segments, layouts, and loading states, improving navigation speed and user experience. This cache is session-based and cleared on page refresh; it can also be automatically invalidated after a set period. |
Beta Was this translation helpful? Give feedback.
By default, fetch requests in Server Components are cached on the server (“Data Cache”).
On the first request, Next.js fetches data from the remote source and stores it in the Data Cache.
Subsequent requests for the same resource will return the cached data, as long as it’s still fresh.
If the cache is stale or missing, Next.js fetches new data and updates the cache.
You can control caching per request with the cache option:
cache: 'force-cache' (default): Uses the Data Cache if possible.
cache: 'no-store': Always fetches fresh data and does not cache the response.
Revalidation:
You can set a next.revalidate option to specify how often cached data should be considered stale and refetched.
T…