ISR lets your Next.js site update static pages after you build it, without rebuilding the whole site. This keeps pages fast and fresh.
ISR (Incremental Static Regeneration) in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NextJS
export async function getStaticProps() { // fetch data here const data = {}; // example placeholder return { props: { data }, revalidate: 10, // seconds }; }
The revalidate key tells Next.js how often to update the page.
When a user visits after revalidate seconds, Next.js regenerates the page in the background.
Examples
NextJS
export async function getStaticProps() { const data = await fetch('https://api.example.com/posts').then(res => res.json()); return { props: { posts: data }, revalidate: 60, // update every 60 seconds }; }
NextJS
export async function getStaticProps() { return { props: { message: 'Hello!' }, revalidate: 5, // update every 5 seconds }; }
Sample Program
This Next.js page shows the current time when the page was generated. It updates every 10 seconds using ISR.
NextJS
import React from 'react'; export default function TimePage({ time }) { return ( <main> <h1>Current Time</h1> <p>The page was generated at: {time}</p> </main> ); } export async function getStaticProps() { const time = new Date().toLocaleTimeString(); return { props: { time }, revalidate: 10, // regenerate page every 10 seconds }; }
Important Notes
ISR helps balance speed and freshness by updating pages in the background.
If revalidate is not set, the page is only generated once at build time.
ISR works only with getStaticProps, not with server-side rendering.
Summary
ISR updates static pages after build without full rebuilds.
Use revalidate in getStaticProps to set update frequency.
It keeps pages fast and content fresh automatically.
Practice
1. What is the main purpose of Incremental Static Regeneration (ISR) in Next.js?
easy
Solution
Step 1: Understand ISR concept
ISR allows static pages to be updated after the initial build without rebuilding the whole site.Step 2: Compare options
Options B, C, and D describe client-side rendering, disabling static generation, or forcing server-side rendering, which are not ISR features.Final Answer:
To update static pages after build without rebuilding the entire site -> Option CQuick Check:
ISR updates static pages incrementally = A [OK]
Hint: ISR updates static pages without full rebuilds [OK]
Common Mistakes:
- Confusing ISR with client-side rendering
- Thinking ISR disables static generation
- Believing ISR forces server-side rendering
2. Which Next.js function should you use to enable ISR by specifying a revalidation time?
easy
Solution
Step 1: Identify ISR enabling function
ISR is enabled by returning arevalidateproperty insidegetStaticProps.Step 2: Check other options
getServerSidePropsis for server-side rendering,getInitialPropsis legacy, anduseEffectis a React hook for client-side effects.Final Answer:
getStaticProps -> Option BQuick Check:
ISR uses getStaticProps with revalidate = D [OK]
Hint: Use getStaticProps with revalidate for ISR [OK]
Common Mistakes:
- Using getServerSideProps instead of getStaticProps
- Confusing client-side hooks with data fetching
- Using deprecated getInitialProps for ISR
3. Given this code snippet, what will be the behavior of the page?
export async function getStaticProps() {
return {
props: { time: Date.now() },
revalidate: 10,
};
}medium
Solution
Step 1: Analyze revalidate property
Therevalidate: 10means Next.js will regenerate the page at most every 10 seconds.Step 2: Understand page update behavior
The page will serve the static content initially, then update the static page in the background every 10 seconds.Final Answer:
The page updates every 10 seconds with new time -> Option DQuick Check:
revalidate 10 means update every 10 seconds = A [OK]
Hint: revalidate sets update interval in seconds [OK]
Common Mistakes:
- Thinking page never updates after build
- Confusing ISR with server-side rendering
- Assuming syntax error due to revalidate
4. Identify the error in this ISR setup:
export async function getStaticProps() {
return {
props: { data: 'Hello' },
revalidate: '60',
};
}medium
Solution
Step 1: Check revalidate type
Therevalidateproperty must be a number representing seconds, not a string.Step 2: Verify other parts
propsis correctly an object,getStaticPropscan be async, and fallback is unrelated here.Final Answer:
revalidate must be a number, not a string -> Option AQuick Check:
revalidate type must be number = C [OK]
Hint: revalidate must be numeric seconds, not string [OK]
Common Mistakes:
- Passing revalidate as string instead of number
- Thinking getStaticProps can't be async
- Confusing fallback with revalidate
5. You want a blog page to update its static content every 5 minutes but also show a fallback loading page on first visit to new posts. Which ISR setup is correct?
hard
Solution
Step 1: Understand ISR with fallback
To update static pages every 5 minutes (300 seconds) and show fallback loading for new pages, userevalidate: 300ingetStaticPropsandfallback: 'blocking'ingetStaticPaths.Step 2: Check other options
UsegetServerSidePropswithrevalidate: 300uses server-side rendering, not ISR. UsegetStaticPropswithrevalidate: '300'and no fallback has revalidate as string and no fallback. UsegetStaticPathswithfallback: falseand no revalidate disables fallback and revalidate, so no ISR updates or loading fallback.Final Answer:
Use getStaticProps with revalidate: 300 and fallback: 'blocking' in getStaticPaths -> Option AQuick Check:
ISR with fallback blocking and revalidate 300 = B [OK]
Hint: Combine revalidate with fallback: 'blocking' for ISR + loading [OK]
Common Mistakes:
- Using server-side rendering instead of ISR
- Passing revalidate as string
- Setting fallback to false disables loading fallback
