What if your site could update itself automatically without slowing down visitors?
Why Revalidation strategies (time-based) in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a website that shows news articles. Every time someone visits, you fetch fresh data manually by calling the server. If many people visit at once, your server gets overwhelmed and the site feels slow.
Manually fetching fresh data on every visit is slow and costly. It can cause delays, overload servers, and users might see outdated content if caching is not handled well.
Time-based revalidation lets Next.js automatically refresh cached pages after a set time. This keeps content fresh without slowing down users or overloading servers.
fetch('/api/data') on every page loadexport const revalidate = 60; // refresh data every 60 seconds
This makes your site fast, scalable, and always up-to-date without extra work.
A blog that updates its homepage every minute to show the latest posts without making visitors wait.
Manual data fetching on every visit is slow and costly.
Time-based revalidation automates refreshing cached content.
This keeps sites fast and fresh for all users.
Practice
revalidate property in getStaticProps do in Next.js?Solution
Step 1: Understand
Therevalidaterole ingetStaticPropsrevalidateproperty sets a time interval in seconds for Next.js to regenerate the static page in the background.Step 2: Effect of setting
After the specified time, Next.js updates the static page automatically without manual rebuilds or disabling static generation.revalidateFinal Answer:
It tells Next.js to update the static page automatically after the specified seconds. -> Option AQuick Check:
Time-based revalidation = automatic page update [OK]
- Thinking revalidate disables static generation
- Confusing revalidate with client-side fetching
- Assuming revalidate caches forever
getStaticProps?Solution
Step 1: Check the type of
Therevalidaterevalidatevalue must be a number representing seconds.Step 2: Validate each option's syntax
export async function getStaticProps() { return { props: {}, revalidate: 10 } } uses a number 10 correctly. export async function getStaticProps() { return { props: {}, revalidate: '10' } } uses a string '10' which is invalid. The other options use boolean and null, which are incorrect types.Final Answer:
export async function getStaticProps() { return { props: {}, revalidate: 10 } } -> Option DQuick Check:
revalidate must be a number [OK]
- Using string instead of number for revalidate
- Setting revalidate to true or null
- Forgetting to return revalidate inside the returned object
getStaticProps:
export async function getStaticProps() {
return {
props: { time: Date.now() },
revalidate: 5
}
}
What will happen if you visit the page multiple times within 3 seconds?Solution
Step 1: Understand revalidate timing
Therevalidate: 5means Next.js regenerates the page at most every 5 seconds.Step 2: Behavior within 3 seconds
Visiting within 3 seconds means the cached page is served with the sametimevalue because regeneration hasn't happened yet.Final Answer:
The page will show the sametimevalue for all visits within 3 seconds. -> Option AQuick Check:
Revalidate interval controls update frequency [OK]
- Expecting page to update on every visit
- Thinking revalidate causes errors if too small
- Assuming page never updates after first build
revalidate: 0 in getStaticProps. What is the problem with this code?Solution
Step 1: Understand
Settingrevalidate: 0meaningrevalidateto 0 disables Incremental Static Regeneration (ISR). The page is generated at build time and cached forever without background regeneration.Step 2: Effect on page behavior
This results in no automatic updates, which is the problem if revalidation was intended, behaving like static generation without ISR.Final Answer:
It caches the page forever without updates. -> Option BQuick Check:
revalidate 0 = no ISR, cache forever [OK]
- Thinking revalidate: 0 regenerates on every request
- Believing it causes a build error
- Assuming it prevents the page from rendering
revalidate helps achieve this efficiently?Solution
Step 1: Understand ISR with
Incremental Static Regeneration (ISR) allows pages to update after a set time without full rebuilds.revalidateStep 2: Combine with conditional data fetching
Fetching data conditionally insidegetStaticPropsensures updates only when content changes, saving resources.Final Answer:
Userevalidate: 60with Incremental Static Regeneration (ISR) and conditional data fetching. -> Option CQuick Check:
ISR + revalidate = efficient timed updates [OK]
- Using getServerSideProps which disables static caching
- Setting revalidate to false which is invalid
- Using revalidate 0 causing regen every request
