Consider this Next.js Server Component code snippet that fetches data:
const data = await fetch('https://api.example.com/data', { cache: 'no-store' }).then(res => res.json());What is the effect of cache: 'no-store' in this fetch call?
const data = await fetch('https://api.example.com/data', { cache: 'no-store' }).then(res => res.json());
Think about what 'no-store' means in HTTP caching terms.
The cache: 'no-store' option tells Next.js to never cache the response and always fetch fresh data on every request.
Given this Next.js fetch call in a Server Component:
const data = await fetch('https://api.example.com/data', { next: { revalidate: 10 } }).then(res => res.json());What happens when this component is requested multiple times within 10 seconds?
const data = await fetch('https://api.example.com/data', { next: { revalidate: 10 } }).then(res => res.json());
Consider what 'revalidate' means in Next.js fetch options.
The revalidate: 10 option caches the response and refreshes it after 10 seconds. Requests within 10 seconds use cached data.
Which fetch call in Next.js will cause a syntax or runtime error due to incorrect cache option usage?
Check the allowed string values for the cache option in fetch.
The cache option accepts only specific strings like 'force-cache', 'reload', 'no-cache', and 'no-store'. 'invalid-cache' is not valid and causes an error.
A developer uses this fetch call in a Next.js Server Component:
const data = await fetch('https://api.example.com/data', { next: { revalidate: 5 } }).then(res => res.json());But the data never updates even after 10 seconds. What is the most likely cause?
Think about how HTTP headers can affect caching behavior.
If the API sends headers like 'Cache-Control: no-cache', it can prevent Next.js from caching the response despite the revalidate option.
You are building a Next.js app that shows personalized user data that changes frequently. Which fetch cache option is best to ensure users always see fresh data without unnecessary delays?
Consider the need for fresh personalized data versus caching benefits.
For frequently changing personalized data, cache: 'no-store' ensures fresh data every time, avoiding stale cache issues.