Revalidation strategies help keep your website data fresh by updating it automatically after a set time. This way, users see recent content without waiting for a full rebuild.
Revalidation strategies (time-based) in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
export async function getStaticProps() { // fetch data here return { props: { data }, revalidate: 10, // seconds }; }
The revalidate value is in seconds and tells Next.js how often to update the page.
Next.js will serve the cached page until the revalidation time passes, then update in the background.
export async function getStaticProps() { return { props: { message: 'Hello!' }, revalidate: 60, // update every 60 seconds }; }
export async function getStaticProps() { return { props: { time: new Date().toISOString() }, revalidate: 5, // update every 5 seconds }; }
This Next.js page shows the time it was generated. It updates automatically every 10 seconds using time-based revalidation.
import React from 'react'; export async function getStaticProps() { const currentTime = new Date().toISOString(); return { props: { currentTime }, revalidate: 10, // revalidate every 10 seconds }; } export default function TimePage({ currentTime }) { return ( <main> <h1>Current Time</h1> <p>The page was generated at:</p> <time dateTime={currentTime}>{currentTime}</time> </main> ); }
Revalidation happens in the background, so users see the old page until the new one is ready.
If you set revalidate to 0 or omit it, the page will not update automatically.
Use time-based revalidation to balance performance and freshness.
Time-based revalidation updates static pages automatically after a set number of seconds.
This keeps content fresh without rebuilding the whole site manually.
Set revalidate in getStaticProps to control update frequency.
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
