Cache invalidation strategies help keep your app's data fresh by deciding when to update or remove stored data. This avoids showing old information to users.
Cache invalidation strategies in NextJS
export async function getStaticProps() { const data = await fetchData() return { props: { data }, revalidate: 60 // seconds } }
The revalidate key tells Next.js to update the cached page after the given seconds.
This is called Incremental Static Regeneration (ISR) in Next.js.
export async function getStaticProps() { const data = await fetch('https://api.example.com/posts').then(res => res.json()) return { props: { posts: data }, revalidate: 10 // update cache every 10 seconds } }
export async function getServerSideProps() { const data = await fetch('https://api.example.com/user').then(res => res.json()) return { props: { user: data } } }
export async function getStaticProps() { const data = await fetchData() return { props: { data } } }
This Next.js page fetches a post and caches it. The cache updates every 30 seconds, so users see fresh content without slowing down the page.
import React from 'react' export async function getStaticProps() { const res = await fetch('https://jsonplaceholder.typicode.com/posts/1') const post = await res.json() return { props: { post }, revalidate: 30 // refresh cache every 30 seconds } } export default function Post({ post }) { return ( <main> <h1>{post.title}</h1> <p>{post.body}</p> </main> ) }
Use revalidate in getStaticProps for automatic cache updates.
getServerSideProps always fetches fresh data on each request, so no caching is done.
Choose cache times based on how often your data changes and how fresh it needs to be.
Cache invalidation keeps your app data fresh and fast.
Next.js uses revalidate in getStaticProps for incremental updates.
Use getServerSideProps for always fresh data without caching.
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
