0
0
NextJSframework~20 mins

Fetch caching behavior in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fetch Cache Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding fetch cache option in Next.js Server Components

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?

NextJS
const data = await fetch('https://api.example.com/data', { cache: 'no-store' }).then(res => res.json());
AIt caches the response indefinitely and never refetches.
BIt caches the response for 60 seconds before refetching.
CIt disables caching and fetches fresh data on every request.
DIt uses the browser's default caching behavior.
Attempts:
2 left
💡 Hint

Think about what 'no-store' means in HTTP caching terms.

state_output
intermediate
2:00remaining
Effect of revalidation time on fetch caching

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?

NextJS
const data = await fetch('https://api.example.com/data', { next: { revalidate: 10 } }).then(res => res.json());
AThe data is cached indefinitely and never refreshed.
BThe data is fetched once and cached for 10 seconds; subsequent requests within 10 seconds use cached data.
CThe data is fetched fresh on every request regardless of time.
DThe fetch call throws an error because 'revalidate' is invalid.
Attempts:
2 left
💡 Hint

Consider what 'revalidate' means in Next.js fetch options.

📝 Syntax
advanced
2:00remaining
Identify the invalid fetch cache option usage

Which fetch call in Next.js will cause a syntax or runtime error due to incorrect cache option usage?

Afetch('https://api.example.com/data', { cache: 'no-cache' })
Bfetch('https://api.example.com/data', { cache: 'reload' })
Cfetch('https://api.example.com/data', { cache: 'force-cache' })
Dfetch('https://api.example.com/data', { cache: 'invalid-cache' })
Attempts:
2 left
💡 Hint

Check the allowed string values for the cache option in fetch.

🔧 Debug
advanced
2:00remaining
Why does fetch cache not update after revalidate time?

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?

AThe API response includes headers that prevent caching, overriding revalidate.
BThe 'revalidate' option only works in Client Components, not Server Components.
CThe fetch call is missing the 'cache: no-store' option to force refresh.
DThe Next.js app is running in development mode where caching is disabled.
Attempts:
2 left
💡 Hint

Think about how HTTP headers can affect caching behavior.

🧠 Conceptual
expert
3:00remaining
Choosing fetch cache strategy for dynamic user data

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?

AUse <code>cache: 'no-store'</code> to always fetch fresh data on every request.
BUse <code>next: { revalidate: 60 }</code> to cache data for 60 seconds before refreshing.
CUse <code>cache: 'force-cache'</code> to cache data indefinitely for performance.
DUse <code>cache: 'reload'</code> to reload data only when the browser cache expires.
Attempts:
2 left
💡 Hint

Consider the need for fresh personalized data versus caching benefits.