ISR Mechanics
Incremental Static Regeneration (ISR) Mechanics¶
ISR (Incremental Static Regeneration) is a hybrid rendering strategy in Next.js that combines the performance benefits of static site generation (SSG) with the flexibility of server-side rendering (SSR). It allows pages to be pre-rendered at build time but regenerated on-demand after deployment, balancing freshness and efficiency for dynamic content.
How ISR Works¶
- Initial Build: Pages are generated as static HTML during the build phase using
getStaticPropsandgetStaticPaths. - Deployment: The static HTML is served to clients, ensuring fast load times.
- Revalidation Triggers:
- Automatic: Pages are regenerated after a configured
revalidateinterval (e.g., 60 seconds). - Manual: A POST request to the
/api/revalidateendpoint triggers regeneration for a specific path. - On-Demand Regeneration: When a trigger occurs, Next.js re-renders the page server-side, updates the static file, and caches the new version.
Example: A blog post page (/posts/123) is pre-rendered during the build. If the post is updated, ISR regenerates the page after 60 seconds or when a revalidation request is made.
Use Cases¶
- Frequently Updated Content: Blogs, product inventories, or user-generated content that doesn’t require real-time updates.
- Dynamic Data with Long Cache TTLs: News sites, dashboards, or marketing pages where freshness is important but not immediate.
- Scalable Content Delivery: Reduces server load by caching static pages while allowing periodic updates.
Why Not SSR? ISR avoids the overhead of SSR for static content, while still supporting updates without full rebuilds.
Triggering Regeneration¶
1. Automatic Revalidation¶
Set the revalidate parameter in getStaticProps to specify the regeneration interval (in seconds):
export async function getStaticProps() {
const data = await fetchData();
return {
props: { data },
revalidate: 60, // Regenerate every 60 seconds
};
}
2. Manual Revalidation via API¶
Create a serverless function to trigger regeneration for a specific path:
// pages/api/revalidate.js
export default async function handler(req, res) {
if (req.method === 'POST') {
const { path } = req.body;
// Trigger regeneration for the specified path
res.status(200).json({ revalidated: true });
} else {
res.status(405).json({ error: 'Method not allowed' });
}
}
Techniques for Optimizing ISR¶
- Conditional Revalidation: Use
revalidate: 0to force regeneration on every request (useful for highly dynamic content). - Next.js Revalidate Package: Use
next-revalidateto programmatically trigger revalidation from client-side code. - Route Grouping: Group related routes (e.g.,
/posts/*) to minimize revalidation overhead. - Caching Strategies: Combine ISR with CDN caching to balance performance and freshness.
Diagram: ISR Workflow¶
[Build Phase] --> [Deploy Static Files]
| |
v v
[Request] --> [Serve Static HTML] [Revalidation Trigger]
| |
v v
[Regenerate on Server] --> [Update Cache] --> [Serve Fresh HTML]
Key takeaways¶
- ISR enables static pages to be regenerated on-demand, balancing performance and freshness.
- Use
revalidateingetStaticPropsfor automatic regeneration or the/api/revalidateendpoint for manual triggers. - Ideal for content that updates infrequently but requires periodic refreshes (e.g., blogs, product data).
- Combine ISR with CDNs and caching layers to optimize delivery for global audiences.
- Avoid overusing ISR for real-time data; use SSR or APIs for dynamic, client-side interactions.