Discover how caching can make your web pages feel instant and smooth!
Why Fetch caching behavior in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a website that fetches data from a server every time a user visits a page, even if the data hasn't changed.
Each visit causes the browser to wait for the server response, slowing down the experience.
Manually handling data fetching means repeated requests for the same data, wasting time and bandwidth.
This leads to slow page loads and a poor user experience.
Fetch caching behavior automatically stores fetched data and reuses it when possible.
This reduces unnecessary network calls and speeds up page loading.
const response = await fetch('/api/data', { cache: 'no-store' }); // fetches every time, no caching const data = await response.json();
const response = await fetch('/api/data', { cache: 'force-cache' }); // uses cached data const data = await response.json();
It enables faster, smoother user experiences by smartly reusing data without extra waiting.
When you refresh a news website, cached articles load instantly instead of waiting for the server each time.
Manual fetching repeats network calls and slows pages.
Fetch caching stores and reuses data automatically.
This improves speed and reduces server load.
Practice
cache: "force-cache" in a fetch call do?Solution
Step 1: Understand the cache option "force-cache"
This option tells Next.js to use cached data if it exists, avoiding a network request.Step 2: Behavior when cache is missing
If no cached data is found, it fetches fresh data and caches it for future use.Final Answer:
It returns cached data if available, otherwise fetches from the network. -> Option AQuick Check:
force-cache = use cache first [OK]
- Confusing force-cache with no-store which disables cache
- Thinking force-cache always fetches fresh data
- Assuming force-cache caches only for session
Solution
Step 1: Identify the option for no caching
Theno-storecache mode disables caching and always fetches fresh data.Step 2: Verify syntax correctness
The syntaxfetch(url, { cache: "no-store" })is valid and correctly disables cache.Final Answer:
fetch(url, { cache: "no-store" }) -> Option DQuick Check:
no-store disables cache [OK]
- Using "reload" which is not a valid cache option in Next.js fetch
- Confusing "force-cache" with no caching
- Omitting the cache option entirely
await fetch('/api/data', { cache: 'no-cache' })Solution
Step 1: Understand the 'no-cache' mode
This mode returns cached data if available but triggers a background fetch to update the cache.Step 2: Confirm behavior in Next.js fetch
Next.js uses this to balance speed and freshness by serving cache immediately and updating it asynchronously.Final Answer:
Returns cached data if available, but revalidates in background. -> Option AQuick Check:
no-cache = cache then revalidate [OK]
- Thinking no-cache disables cache completely
- Assuming no-cache never uses cached data
- Confusing no-cache with no-store
fetch('/api/data', { cache: 'reload' })But it throws an error. What is the problem?
Solution
Step 1: Check valid cache options in Next.js
Next.js fetch supports "force-cache", "no-store", "no-cache", and "default" but not "reload".Step 2: Identify error cause
Using "reload" causes a syntax or runtime error because it's unsupported.Final Answer:
"reload" is not a valid cache option in Next.js fetch. -> Option CQuick Check:
Invalid cache option = error [OK]
- Assuming reload is valid from browser fetch API
- Ignoring error messages about invalid options
- Thinking method or URL causes this error
Solution
Step 1: Understand requirement for freshness and caching
You want fresh data but avoid fetching more than once every 10 seconds.Step 2: Analyze caching options
cache: 'no-cache'serves cached data but revalidates in background;next: { revalidate: 10 }tells Next.js to re-fetch after 10 seconds.Step 3: Compare with other options
no-storedisables cache completely, forcing fetch every time;force-cachecaches indefinitely;defaulthas no revalidation control.Final Answer:
Use cache: 'no-cache' with next: { revalidate: 10 } option. -> Option BQuick Check:
no-cache + revalidate = fresh every 10s [OK]
- Using no-store causes always fresh but no caching
- Using force-cache ignores revalidate timing
- Forgetting to add revalidate option
