Fetch caching helps your app load data faster by saving responses. It avoids asking the server for the same data again and again.
Fetch caching behavior in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
fetch(url, { cache: 'default' | 'no-store' | 'force-cache' | 'only-if-cached' })
cache option controls how fetch uses the cache.
Common values: default (normal), no-store (never cache), force-cache (always use cache if available), only-if-cached (only use cache, fail if not cached).
fetch('/api/data', { cache: 'no-store' })
fetch('/api/data', { cache: 'force-cache' })
fetch('/api/data')
This React component fetches data from an API using cache: 'force-cache'. It shows cached data if available, making loading faster.
import React, { useEffect, useState } from 'react'; export default function CachedFetchExample() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchData() { // Fetch with cache: 'force-cache' to use cached data if available const response = await fetch('/api/hello', { cache: 'force-cache' }); const json = await response.json(); setData(json.message); setLoading(false); } fetchData(); }, []); if (loading) return <p>Loading...</p>; return <p>Message: {data}</p>; } // Assume /api/hello returns { "message": "Hello from API!" }
Using cache: 'no-store' ensures fresh data but can slow down your app.
force-cache is good for data that changes rarely.
Next.js also supports advanced caching with server actions and revalidation, but this is the basic fetch cache control.
Fetch caching controls if data is reused or freshly fetched.
Use cache option to tell fetch how to handle cache.
Choosing the right cache mode helps balance speed and data freshness.
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
