0
0
NextJSframework~20 mins

Cache invalidation strategies in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What happens when you use stale-while-revalidate in Next.js caching?
In Next.js, when you use the stale-while-revalidate cache control header, what is the expected behavior when a cached page is requested after the stale time has passed?
AThe cached page is discarded and a 404 error is returned until the fresh page is generated.
BThe request waits until the fresh page is fetched before serving any content to the user.
CThe cached page is served immediately while Next.js fetches a fresh version in the background to update the cache.
DThe cache is cleared and the user is redirected to the homepage.
Attempts:
2 left
💡 Hint
Think about how stale-while-revalidate balances speed and freshness.
component_behavior
intermediate
2:00remaining
How does Next.js ISR (Incremental Static Regeneration) handle cache invalidation?
Given a Next.js page using ISR with revalidate: 10, what happens when a user requests the page after 15 seconds since the last generation?
NextJS
export async function getStaticProps() {
  return {
    props: { time: Date.now() },
    revalidate: 10
  };
}
ANext.js serves the cached page immediately and triggers a background regeneration of the page.
BNext.js waits for the page to regenerate before serving it to the user.
CNext.js serves a 404 error because the cache expired.
DNext.js disables caching and regenerates the page on every request.
Attempts:
2 left
💡 Hint
ISR allows pages to update without rebuilding the whole site.
📝 Syntax
advanced
2:00remaining
Which Next.js cache control header syntax correctly sets a 1-hour max-age with stale-while-revalidate?
Choose the correct way to set the Cache-Control header in Next.js API route to cache for 1 hour and allow stale content while revalidating.
NextJS
export default function handler(req, res) {
  // Set cache control header here
  res.status(200).json({ message: 'Hello' });
}
Ares.setHeader('Cache-Control', 'public max-age=3600 stale-while-revalidate=3600');
Bres.setHeader('Cache-Control', 'max-age=3600 stale-while-revalidate=3600');
Cres.setHeader('Cache-Control', 'public, max-age=3600; stale-while-revalidate=3600');
Dres.setHeader('Cache-Control', 'public, max-age=3600, stale-while-revalidate=3600');
Attempts:
2 left
💡 Hint
Headers use commas to separate directives.
🔧 Debug
advanced
2:00remaining
Why does this Next.js ISR page never update after deployment?
This Next.js page uses ISR with revalidate: 60. After deployment, the page content never changes even after 5 minutes. What is the most likely cause?
NextJS
export async function getStaticProps() {
  const data = await fetch('https://api.example.com/data').then(res => res.json());
  return {
    props: { data },
    revalidate: 60
  };
}
AThe <code>revalidate</code> value must be a string, not a number.
BThe API response is cached and does not change, so the page content stays the same.
CISR only works in development mode, not in production.
DThe <code>getStaticProps</code> function must return <code>revalidate</code> inside <code>props</code>.
Attempts:
2 left
💡 Hint
Consider what controls the data freshness besides ISR timing.
state_output
expert
2:00remaining
What is the output of this Next.js API route with cache headers and multiple requests?
Consider this Next.js API route that sets cache headers and returns a timestamp. What will be the output of two requests made 5 seconds apart?
NextJS
let lastGenerated = 0;

export default function handler(req, res) {
  const now = Date.now();
  if (now - lastGenerated > 10000) {
    lastGenerated = now;
  }
  res.setHeader('Cache-Control', 'public, max-age=10');
  res.status(200).json({ timestamp: lastGenerated });
}
ABoth requests return the same timestamp because the cache max-age is 10 seconds.
BThe second request returns 0 because the cache expired and lastGenerated reset.
CBoth requests return different timestamps because the handler updates on every call.
DThe first request returns 0, the second returns a new timestamp 5 seconds later.
Attempts:
2 left
💡 Hint
Think about how max-age controls browser and CDN caching behavior.