Bird
Raised Fist0
NextJSframework~10 mins

Cache invalidation strategies in NextJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fetch fresh data by disabling cache in Next.js fetch.

NextJS
const data = await fetch('/api/data', { cache: '[1]' });
Drag options to blanks, or click blank then click option'
A'force-cache'
B'only-if-cached'
C'default'
D'no-store'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'force-cache' will use cached data instead of fresh data.
Using 'default' may use cache depending on browser or server settings.
2fill in blank
medium

Complete the code to revalidate cached data every 60 seconds in Next.js.

NextJS
const data = await fetch('/api/data', { next: { revalidate: [1] } });
Drag options to blanks, or click blank then click option'
A60
B30
C120
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting revalidate to 0 disables caching completely.
Choosing too high a number delays cache updates.
3fill in blank
hard

Fix the error in the code to correctly revalidate cache after 10 seconds.

NextJS
const data = await fetch('/api/data', { next: { revalidate: [1] } });
Drag options to blanks, or click blank then click option'
Atrue
B'10s'
C10
D'10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like '10s' causes a runtime error.
Using boolean true is invalid for revalidate.
4fill in blank
hard

Fill both blanks to create a cache key and set stale-while-revalidate header.

NextJS
export async function GET() {
  const cacheKey = [1];
  return new Response('data', {
    headers: { 'Cache-Control': 'max-age=0, [2]=60' }
  });
}
Drag options to blanks, or click blank then click option'
A'user-data'
B'stale-while-revalidate'
C'max-age'
D'no-cache'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'max-age' in place of 'stale-while-revalidate' changes cache behavior.
Using 'no-cache' disables caching entirely.
5fill in blank
hard

Fill all three blanks to create a Next.js fetch with cache disabled, revalidate after 30 seconds, and use a custom cache key.

NextJS
const data = await fetch('/api/data', {
  cache: [1],
  next: { revalidate: [2] },
  headers: { 'x-cache-key': [3] }
});
Drag options to blanks, or click blank then click option'
A'no-store'
B30
C'custom-key-123'
D'force-cache'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'force-cache' enables caching, which is not desired here.
Setting revalidate to 0 disables revalidation.
Providing a non-string for the cache key header causes errors.

Practice

(1/5)
1. In Next.js, which method allows you to update static pages after a certain time interval to keep data fresh without rebuilding the entire site?
easy
A. getServerSideProps without caching
B. Manually clearing browser cache
C. Using client-side useEffect to fetch data
D. getStaticProps with revalidate

Solution

  1. Step 1: Understand Next.js data fetching methods

    getStaticProps generates static pages at build time, but with revalidate, it can update pages after a set interval.
  2. Step 2: Compare with other methods

    getServerSideProps fetches fresh data on every request but does not use caching or revalidation intervals.
  3. Final Answer:

    getStaticProps with revalidate -> Option D
  4. Quick Check:

    Incremental Static Regeneration = getStaticProps + revalidate [OK]
Hint: Remember: revalidate updates static pages periodically [OK]
Common Mistakes:
  • Confusing getServerSideProps with revalidation
  • Thinking client-side fetching is cache invalidation
  • Assuming manual browser cache clearing affects server cache
2. Which of the following is the correct way to set a revalidation time of 10 seconds in getStaticProps?
easy
A. return { props: {}, revalidate: 10 }
B. return { props: {}, revalidateTime: 10 }
C. return { props: {}, cache: 10 }
D. return { props: {}, refresh: 10 }

Solution

  1. Step 1: Recall the correct property name for revalidation

    The Next.js documentation specifies revalidate as the property to set seconds for incremental static regeneration.
  2. Step 2: Check the return object structure

    The return object from getStaticProps must include props and optionally revalidate with a number value.
  3. Final Answer:

    return { props: {}, revalidate: 10 } -> Option A
  4. Quick Check:

    Property name is revalidate [OK]
Hint: Use exact property name 'revalidate' in return object [OK]
Common Mistakes:
  • Using incorrect property names like 'revalidateTime' or 'refresh'
  • Placing revalidate outside the return object
  • Setting revalidate as a string instead of a number
3. Consider this Next.js code snippet:
export async function getStaticProps() {
  return {
    props: { time: Date.now() },
    revalidate: 5
  };
}

What will happen if you visit this page multiple times within 5 seconds?
medium
A. The page will throw an error due to Date.now() in getStaticProps
B. The page will show the same timestamp for all visits within 5 seconds
C. The page will update the timestamp on every visit
D. The page will never update the timestamp

Solution

  1. Step 1: Understand revalidate behavior

    The revalidate: 5 means Next.js will regenerate the page at most every 5 seconds.
  2. Step 2: Predict output within 5 seconds

    Within 5 seconds, the cached page is served, so the timestamp remains the same for all visits.
  3. Final Answer:

    The page will show the same timestamp for all visits within 5 seconds -> Option B
  4. Quick Check:

    Revalidate interval caches page for 5 seconds [OK]
Hint: Revalidate caches page until interval expires [OK]
Common Mistakes:
  • Assuming timestamp updates on every visit
  • Thinking Date.now() causes errors in static props
  • Believing revalidate disables caching
4. You wrote this code in Next.js:
export async function getStaticProps() {
  return {
    props: { data: await fetchData() },
    revalidate: '10'
  };
}

Why does this cause a problem?
medium
A. Because revalidate must be a number, not a string
B. Because fetchData cannot be awaited in getStaticProps
C. Because props must be a function, not an object
D. Because revalidate cannot be used with getStaticProps

Solution

  1. Step 1: Check the type of revalidate

    The revalidate property expects a number representing seconds, not a string.
  2. Step 2: Understand impact of wrong type

    Passing a string causes Next.js to throw a type error or ignore the revalidation, breaking cache invalidation.
  3. Final Answer:

    revalidate must be a number, not a string -> Option A
  4. Quick Check:

    Revalidate value type = number [OK]
Hint: Use number type for revalidate seconds [OK]
Common Mistakes:
  • Passing revalidate as a string
  • Thinking async/await is disallowed in getStaticProps
  • Confusing props structure requirements
5. You want to always serve fresh data on every request in Next.js without caching. Which method should you use and why?
hard
A. Use getStaticProps without revalidate
B. Use getStaticProps with revalidate: 0
C. Use getServerSideProps because it runs on every request
D. Use client-side fetching only with useEffect

Solution

  1. Step 1: Identify method for fresh data every request

    getServerSideProps runs on the server for every page request, ensuring fresh data.
  2. Step 2: Compare with static methods

    getStaticProps generates static pages at build time or with revalidation intervals, so it does not guarantee fresh data every request.
  3. Final Answer:

    Use getServerSideProps because it runs on every request -> Option C
  4. Quick Check:

    Fresh data every request = getServerSideProps [OK]
Hint: Always fresh data? Use getServerSideProps [OK]
Common Mistakes:
  • Using revalidate: 0 expecting no caching
  • Relying only on client-side fetching for SEO
  • Confusing static and server-side methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard