0
0
NextJSframework~20 mins

Data cache behavior in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Data Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Next.js cache data in Server Components?

Consider a Next.js Server Component that fetches data from an API using fetch with cache: 'force-cache'. What happens when the component is rendered multiple times within 10 seconds?

NextJS
export default async function Page() {
  const res = await fetch('https://api.example.com/data', { cache: 'force-cache' });
  const data = await res.json();
  return <pre>{JSON.stringify(data)}</pre>;
}
AThe data is fetched once and cached indefinitely; subsequent renders use the cached data.
BThe data is cached only on the client side, not on the server.
CThe data is cached indefinitely and never refetched unless the server restarts.
DThe data is fetched every time the component renders, ignoring the cache option.
Attempts:
2 left
💡 Hint

Think about how cache: 'force-cache' enables server-side caching.

state_output
intermediate
2:00remaining
What is the output when using stale-while-revalidate caching?

Given this Next.js Server Component code snippet, what will the user see immediately after the first render if the cache is stale?

NextJS
export default async function Page() {
  const res = await fetch('https://api.example.com/data', { next: { revalidate: 30 } });
  const data = await res.json();
  return <pre>{JSON.stringify(data)}</pre>;
}
AThe user sees empty data until the cache expires.
BThe user waits for fresh data to load before anything is displayed.
CThe user sees an error because stale-while-revalidate is not supported in Next.js fetch.
DThe user sees the cached data immediately, and the data is updated in the background for the next render.
Attempts:
2 left
💡 Hint

Recall how stale-while-revalidate works in caching strategies.

🔧 Debug
advanced
2:00remaining
Why does data not update despite revalidating in Next.js?

Look at this Next.js Server Component fetching data with revalidate: 5. The data does not update after 5 seconds as expected. What is the likely cause?

NextJS
export default async function Page() {
  const res = await fetch('https://api.example.com/data', { next: { revalidate: 5 } });
  const data = await res.json();
  return <pre>{JSON.stringify(data)}</pre>;
}
AThe <code>revalidate</code> option is misspelled and ignored by Next.js.
BNext.js requires <code>cache: 'no-store'</code> to enable revalidation.
CThe API response is cached by a CDN or proxy, preventing fresh data from reaching Next.js.
DThe component must be a Client Component to use <code>revalidate</code>.
Attempts:
2 left
💡 Hint

Consider external factors affecting data freshness beyond Next.js settings.

🧠 Conceptual
advanced
2:00remaining
How does Next.js handle caching in Client Components?

Which statement best describes data caching behavior when fetching data inside a Next.js Client Component?

AClient Components automatically cache fetch requests on the server like Server Components.
BClient Components do not cache fetch requests by default; each fetch runs fresh unless manually cached.
CClient Components cache fetch requests indefinitely in the browser cache.
DClient Components use Next.js <code>revalidate</code> option to control caching.
Attempts:
2 left
💡 Hint

Think about where Client Components run and how caching works there.

📝 Syntax
expert
2:00remaining
Which fetch option correctly enables incremental static regeneration in Next.js?

Choose the correct fetch option syntax to enable ISR with a 60-second revalidation in a Next.js Server Component.

Afetch(url, { next: { revalidate: 60 } })
Bfetch(url, { cache: 'no-store', revalidate: 60 })
Cfetch(url, { revalidate: 60 })
Dfetch(url, { cache: 'force-cache', revalidate: 60 })
Attempts:
2 left
💡 Hint

Check the official Next.js fetch options for ISR.