What if your website could refresh only what's needed, instantly and without errors?
Why Cache invalidation strategies in NextJS? - Purpose & Use Cases
Imagine you have a website showing product prices that change often. You manually update the cached data every time a price changes by clearing the entire cache or refreshing pages one by one.
Manually clearing cache is slow and error-prone. Sometimes old prices still show, or users see blank pages while cache updates. It's hard to know what to clear and when, causing bad user experience.
Cache invalidation strategies let you automatically update or remove only the outdated cached data. This keeps your site fast and accurate without manual work or errors.
cache.clearAll(); // clears everything, even fresh data
cache.invalidate('product-123-price'); // clears only outdated priceYou can keep your site lightning fast and always up-to-date without confusing manual cache resets.
An online store updates just the changed product info in cache after a price change, so customers see fresh prices instantly without slowing down the whole site.
Manual cache clearing is slow and risky.
Cache invalidation targets only outdated data.
This keeps apps fast and reliable automatically.
Practice
Solution
Step 1: Understand Next.js data fetching methods
getStaticPropsgenerates static pages at build time, but withrevalidate, it can update pages after a set interval.Step 2: Compare with other methods
getServerSidePropsfetches fresh data on every request but does not use caching or revalidation intervals.Final Answer:
getStaticPropswithrevalidate-> Option DQuick Check:
Incremental Static Regeneration =getStaticProps+revalidate[OK]
- Confusing
getServerSidePropswith revalidation - Thinking client-side fetching is cache invalidation
- Assuming manual browser cache clearing affects server cache
getStaticProps?Solution
Step 1: Recall the correct property name for revalidation
The Next.js documentation specifiesrevalidateas the property to set seconds for incremental static regeneration.Step 2: Check the return object structure
The return object fromgetStaticPropsmust includepropsand optionallyrevalidatewith a number value.Final Answer:
return { props: {}, revalidate: 10 } -> Option AQuick Check:
Property name isrevalidate[OK]
- Using incorrect property names like 'revalidateTime' or 'refresh'
- Placing revalidate outside the return object
- Setting revalidate as a string instead of a number
export async function getStaticProps() {
return {
props: { time: Date.now() },
revalidate: 5
};
}What will happen if you visit this page multiple times within 5 seconds?
Solution
Step 1: Understand revalidate behavior
Therevalidate: 5means Next.js will regenerate the page at most every 5 seconds.Step 2: Predict output within 5 seconds
Within 5 seconds, the cached page is served, so the timestamp remains the same for all visits.Final Answer:
The page will show the same timestamp for all visits within 5 seconds -> Option BQuick Check:
Revalidate interval caches page for 5 seconds [OK]
- Assuming timestamp updates on every visit
- Thinking Date.now() causes errors in static props
- Believing revalidate disables caching
export async function getStaticProps() {
return {
props: { data: await fetchData() },
revalidate: '10'
};
}Why does this cause a problem?
Solution
Step 1: Check the type of
Therevalidaterevalidateproperty expects a number representing seconds, not a string.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.Final Answer:
revalidatemust be a number, not a string -> Option AQuick Check:
Revalidate value type = number [OK]
- Passing revalidate as a string
- Thinking async/await is disallowed in getStaticProps
- Confusing props structure requirements
Solution
Step 1: Identify method for fresh data every request
getServerSidePropsruns on the server for every page request, ensuring fresh data.Step 2: Compare with static methods
getStaticPropsgenerates static pages at build time or with revalidation intervals, so it does not guarantee fresh data every request.Final Answer:
UsegetServerSidePropsbecause it runs on every request -> Option CQuick Check:
Fresh data every request =getServerSideProps[OK]
- Using revalidate: 0 expecting no caching
- Relying only on client-side fetching for SEO
- Confusing static and server-side methods
Ready to prove it?
Pass the test and this pattern moves unknown
