Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Fetch Caching Behavior in Next.js
📖 Scenario: You are building a Next.js app that fetches user data from an API. To improve performance, you want to control how the data is cached when fetching it.
🎯 Goal: Create a Next.js Server Component that fetches user data from https://jsonplaceholder.typicode.com/users with caching disabled, then add a cache duration, and finally enable revalidation to update the cache every 10 seconds.
📋 What You'll Learn
Create a Server Component named UsersList that fetches user data with cache: 'no-store' option.
Add a variable cacheDuration set to 60000 (milliseconds).
Modify the fetch to use next: { revalidate: 10 } option to enable cache revalidation every 10 seconds.
Export the UsersList component as default.
💡 Why This Matters
🌍 Real World
Controlling fetch caching in Next.js helps improve app performance and user experience by reducing unnecessary network requests and keeping data fresh.
💼 Career
Understanding fetch caching is important for Next.js developers to optimize data fetching strategies in real-world web applications.
Progress0 / 4 steps
1
Create the initial fetch with caching disabled
Create a Server Component named UsersList. Inside it, use await fetch to get data from 'https://jsonplaceholder.typicode.com/users' with the option { cache: 'no-store' }. Then parse the response as JSON into a variable called users. Return a <ul> element with each user's name inside <li>.
NextJS
Hint
Use fetch with { cache: 'no-store' } to disable caching.
2
Add a cache duration variable
Above the UsersList component, create a constant variable named cacheDuration and set it to 60000 (milliseconds).
NextJS
Hint
Define cacheDuration as a constant with value 60000.
3
Modify fetch to enable cache revalidation every 10 seconds
Inside the UsersList component, change the fetch options to use next: { revalidate: 10 } instead of { cache: 'no-store' }.
NextJS
Hint
Use next: { revalidate: 10 } in fetch options to enable cache revalidation every 10 seconds.
4
Export the UsersList component as default
Ensure the UsersList component is exported as the default export at the bottom of the file.
NextJS
Hint
Make sure the component is exported as default using export default.
Practice
(1/5)
1. In Next.js, what does setting cache: "force-cache" in a fetch call do?
easy
A. It returns cached data if available, otherwise fetches from the network.
B. It always fetches fresh data from the network, ignoring cache.
C. It disables caching completely for the fetch request.
D. It caches the response only for the current session.
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 A
Quick Check:
force-cache = use cache first [OK]
Hint: force-cache means use cache if present, else fetch fresh [OK]
Common Mistakes:
Confusing force-cache with no-store which disables cache
Thinking force-cache always fetches fresh data
Assuming force-cache caches only for session
2. Which of the following is the correct syntax to fetch data with no caching in Next.js?
easy
A. fetch(url, { cache: "force-cache" })
B. fetch(url, { cache: "default" })
C. fetch(url, { cache: "reload" })
D. fetch(url, { cache: "no-store" })
Solution
Step 1: Identify the option for no caching
The no-store cache mode disables caching and always fetches fresh data.
Step 2: Verify syntax correctness
The syntax fetch(url, { cache: "no-store" }) is valid and correctly disables cache.
Final Answer:
fetch(url, { cache: "no-store" }) -> Option D
Quick Check:
no-store disables cache [OK]
Hint: Use cache: "no-store" to disable caching [OK]
Common Mistakes:
Using "reload" which is not a valid cache option in Next.js fetch
Confusing "force-cache" with no caching
Omitting the cache option entirely
3. What will be the behavior of this Next.js fetch call?
await fetch('/api/data', { cache: 'no-cache' })
medium
A. Returns cached data if available, but revalidates in background.
B. Always fetches fresh data and updates the cache.
C. Ignores cache and never stores the response.
D. Fetches from cache only, never from network.
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 A
Quick Check:
no-cache = cache then revalidate [OK]
Hint: no-cache serves cache then refreshes in background [OK]
Common Mistakes:
Thinking no-cache disables cache completely
Assuming no-cache never uses cached data
Confusing no-cache with no-store
4. You wrote this fetch call in Next.js:
fetch('/api/data', { cache: 'reload' })
But it throws an error. What is the problem?
medium
A. You must use async/await with fetch.
B. You forgot to add method: 'GET' in options.
C. "reload" is not a valid cache option in Next.js fetch.
D. The URL must be absolute, not relative.
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 C
Quick Check:
Invalid cache option = error [OK]
Hint: Check cache option spelling and validity [OK]
Common Mistakes:
Assuming reload is valid from browser fetch API
Ignoring error messages about invalid options
Thinking method or URL causes this error
5. You want to fetch user data in Next.js and ensure it is always fresh but also want to avoid unnecessary network requests if the data was fetched less than 10 seconds ago. Which caching strategy should you use?
hard
A. Use cache: 'no-store' and implement a custom timer to refetch every 10 seconds.
B. Use cache: 'no-cache' with next: { revalidate: 10 } option.
C. Use cache: 'force-cache' with next: { revalidate: 10 } option.
D. Use cache: 'default' without revalidation.
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-store disables cache completely, forcing fetch every time; force-cache caches indefinitely; default has no revalidation control.
Final Answer:
Use cache: 'no-cache' with next: { revalidate: 10 } option. -> Option B
Quick Check:
no-cache + revalidate = fresh every 10s [OK]
Hint: Combine no-cache with revalidate for timed freshness [OK]