Design Converter
Education
Last updated on Nov 2, 2023
Last updated on Aug 4, 2023
In the early days of web development, static site generation was the norm. The concept was straightforward: Generate static pages for the entire site and serve them to the user. This approach was simple and fast, as the server only had to return a static file for each valid request. However, as websites became more dynamic and interactive, the limitations of static site generation became apparent.
1export default function Home({ data }) { 2 // Render data... 3}
Incremental static regeneration (ISR) emerged as a solution to these limitations. Instead of generating the entire site simultaneously, ISR allows you to create static pages on demand. This means that the page component is rendered at build time, but you can also update static pages after they have been successfully generated.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { props: { data } }; 5}
With incremental static regeneration, you don't have to regenerate the entire site whenever data changes. Instead, you can specify which pages to update, allowing for more efficient data fetching.
On-demand incremental static regeneration (ISR) takes this concept a step further. Pages are generated at build time with on-demand ISR, just like with traditional ISR. However, the "on-demand" part means that pages are regenerated in the background as users request.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 if (!data) { 5 return { 6 notFound: true, 7 } 8 } 9 return { props: { data } }; 10}
This means that users always see the latest content without waiting for the entire site to be regenerated. After a data change, the first request to a page triggers the on-demand ISR process. The previously generated page is served immediately, while in the background, a new version of the page is generated. Subsequent requests receive the new, updated page.
1export async function getStaticPaths() { 2 // Determine paths at build time 3 const paths = data.map((item) => ({ 4 params: { id: item.id.toString() }, 5 })); 6 return { paths, fallback: true }; 7}
On-demand ISR is a hybrid solution combining the best static site generation and server-side rendering. It allows for fast, up-to-date content delivery without requiring a full rebuild of the entire site.
1export async function getStaticProps({ params }) { 2 // Fetch data for this page... 3 const res = await fetch(`api_endpoint/${params.id}`); 4 const data = await res.json(); 5 return { props: { data } }; 6}
With traditional static site generation, each valid request is met with a static file - A pre-rendered page. This is ideal for sites with content that doesn't change often. However, for sites with frequently updated data, regenerating the entire site for every change is far from practical.
1export default function Blog({ posts }) { 2 // Render blog posts... 3}
Imagine a blog post that is updated with new comments every few minutes. With static site generation, you would need to regenerate the entire site every time a new comment is added to keep that one blog post up to date.
Incremental static regeneration (ISR) was a step in the right direction. It allowed specific pages to be updated without needing a complete site rebuild. This was a game-changer for sites with frequently changing data.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { props: { data } }; 5}
With ISR, you could update static pages based on data changes. This meant the blog post with new comments could be updated independently without affecting the rest of the site.
However, there was still a delay between the data change and the page update even with ISR. This is where on-demand incremental static regeneration comes in.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 if (!data) { 5 return { 6 notFound: true, 7 } 8 } 9 return { props: { data } }; 10}
With on-demand ISR, the stale content is served for the initial request, but the page is regenerated in the background for future requests. This means that the subsequent requests receive the updated page, ensuring the content is always fresh.
1export async function getStaticPaths() { 2 // Determine paths at build time 3 const paths = data.map((item) => ({ 4 params: { id: item.id.toString() }, 5 })); 6 return { paths, fallback: true }; 7}
On-demand ISR combines the best of both worlds - the speed of serving static pages and the flexibility of updating content per page.
On-demand incremental static regeneration (ISR) is an advanced feature that combines the benefits of static site generation and server-side rendering. The mechanism behind on-demand ISR is what makes it so powerful.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { props: { data } }; 5}
The initial request for a page triggers the on-demand ISR process. The server responds with the cached version of the page if it exists. In the background, it begins regenerating the page. The next request for the same page will receive the newly regenerated page.
1export async function getStaticPaths() { 2 // Determine paths at build time 3 const paths = data.map((item) => ({ 4 params: { id: item.id.toString() }, 5 })); 6 return { paths, fallback: true }; 7}
On-demand ISR offers several benefits. First, it ensures that users always receive the latest content. The stale content is served for the initial request, but the page is regenerated in the background for future requests.
1export async function getStaticProps({ params }) { 2 // Fetch data for this page... 3 const res = await fetch(`api_endpoint/${params.id}`); 4 const data = await res.json(); 5 return { props: { data } }; 6}
Second, on-demand ISR reduces the load on your server. Instead of regenerating the entire site whenever data changes, only the pages that need to be updated are regenerated. This can significantly reduce the load on your server and improve your site's performance.
1export default function Blog({ posts }) { 2 // Render blog posts... 3}
Third, on-demand ISR allows for more efficient data fetching. With traditional static site generation, you must fetch all the data for your entire site at build time. With on-demand ISR, you can fetch data per page, making your data fetching more efficient and your pages load quickly.
On-demand incremental static regeneration (ISR) works on the principle of "stale while revalidate". When a request is made to a page, the server returns the cached page (if available), which may be stale. In the background, it triggers the regeneration of the page.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { props: { data } }; 5}
The regeneration process involves re-running the getStaticProps function and rendering the page component with the new props. If the regeneration is successful, the stale page in the cache is replaced with the new page.
1export async function getStaticPaths() { 2 // Determine paths at build time 3 const paths = data.map((item) => ({ 4 params: { id: item.id.toString() }, 5 })); 6 return { paths, fallback: true }; 7}
The cache plays a crucial role in on-demand ISR. The cache stores the statically generated pages, serving them quickly to users. When a page is regenerated, the old page in the cache replaces the new page.
1export async function getStaticProps({ params }) { 2 // Fetch data for this page... 3 const res = await fetch(`api_endpoint/${params.id}`); 4 const data = await res.json(); 5 return { props: { data } }; 6}
If the background regeneration fails (e.g., an invalid token in an API call), the old page remains in the cache. It continues to be served until the regeneration is successful.
To leverage on-demand incremental static regeneration (ISR), we'll be using Next.js , a popular React framework. Start by setting up a new Next.js project.
1npx create-next-app@latest on-demand-isr-blog
This will create a new Next.js application in a directory called on-demand-isr-blog.
Let's create a new page in the pages directory. We'll use the getStaticProps function to fetch data and the revalidate option to enable ISR.
1export async function getStaticProps() { 2 // Fetch data... 3 const res = await fetch('api_endpoint'); 4 const data = await res.json(); 5 6 return { 7 props: { data }, 8 revalidate: 1 9 }; 10}
In this example, revalidate: 1 means that if a request comes in more than one second after the last generated time, Next.js will attempt to regenerate the page in the background while serving the old page.
We need to handle cases where the page is not yet generated to implement on-demand ISR. We can do this by using the fallback option in the getStaticPaths function.
1export async function getStaticPaths() { 2 return { 3 paths: [], 4 fallback: 'blocking' 5 }; 6}
With fallback: 'blocking', new paths not returned by getStaticPaths will wait for the HTML to be generated, identical to server-side rendering (SSR).
When you build your Next.js application, the getStaticPaths function runs first. This function determines which paths will be pre-rendered at build time. The getStaticProps function runs for each path, fetching the data and generating the static page.
1export async function getStaticPaths() { 2 // Determine paths at build time 3 const paths = data.map((item) => ({ 4 params: { id: item.id.toString() }, 5 })); 6 return { paths, fallback: true }; 7}
The server returns the cached page when a request comes in for a pre-rendered page at build time. If the page is due for revalidation (as specified by the revalidate option in getStaticProps), the server triggers the regeneration of the page.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { 5 props: { data }, 6 revalidate: 1 7 }; 8}
The regeneration process involves re-running the getStaticProps function and rendering the page component with the new props. If the regeneration succeeds, the old page in the cache replaces the new one. If the regeneration fails, the old page continues to be served until the regeneration is successful.
While on-demand incremental static regeneration (ISR) is a powerful tool, there are a few common pitfalls to be aware of. One such pitfall is setting the revalidation time too low, which can lead to frequent page regeneration and strain your server.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { 5 props: { data }, 6 revalidate: 1 // This might be too low for most use cases 7 }; 8}
Another common issue is forgetting to handle fallback pages properly. When a page is not pre-rendered and a request comes in, the fallback: true option will immediately return a fallback version of the page. You should ensure that your page component can handle this loading state gracefully.
1export async function getStaticPaths() { 2 return { 3 paths: [], 4 fallback: true // Make sure to handle the loading state in your page component 5 }; 6}
Debugging issues with on-demand ISR can be challenging due to its asynchronous nature. A good starting point is to add logging into your getStaticProps function to see when it's being called.
1export async function getStaticProps() { 2 console.log('getStaticProps called'); 3 // Fetch data... 4 const data = await res.json(); 5 return { 6 props: { data }, 7 revalidate: 60 8 }; 9}
Another useful tip is to use the network tab in your browser's developer tools to inspect the responses from your server. This can help you understand when and why page regeneration is happening.
On-demand incremental static regeneration (ISR) is a relatively new feature in web development. Next.js, a popular React framework, currently supports it. On-demand ISR is gaining traction due to its ability to combine static site generation and server-side rendering.
1export async function getStaticProps() { 2 // Fetch data... 3 const data = await res.json(); 4 return { 5 props: { data }, 6 revalidate: 60 7 }; 8}
On-demand Incremental Static Regeneration (ISR) is a transformative technique that amalgamates the best aspects of static site generation and server-side rendering. It provides the speed and SEO advantages of static pages while offering the flexibility to update content as soon as changes occur.
Throughout this blog post, we've delved deep into the mechanics of on-demand ISR, exploring its principles, benefits, and practical use cases. We've also discussed implementing it in a Next.js project and troubleshooting common issues.
As we witness a growing number of developers discovering the benefits of on-demand ISR, it's clear that its adoption is set to rise. It's an exhilarating time to be a web developer as we continue to see innovative solutions like on-demand ISR that push the boundaries of what's possible.
However, as we progress with these advancements, we encounter new challenges. One such challenge is dealing with APIs. Fetching data, parsing responses, and managing errors can become complex and time-consuming, especially for complicated API endpoints.
This is where WiseGPT , a plugin developed by DhiWise , comes into play. WiseGPT is designed to generate code for APIs into your React project, mirroring your coding style and automatically creating models and functions. It eliminates the need for manual API requests, response parsing, and error management strategies, handling everything for you. And the best part? There's no limit on the output size.
So, as you embark on your journey exploring on-demand ISR, I encourage you also to try out WiseGPT . Tools like these make our lives as developers more accessible, allowing us to focus on what truly matters: creating fantastic web experiences.
Thank you for joining me on this deep dive into on-demand Incremental Static Regeneration. I hope you've found it insightful and inspired you to explore this powerful feature further.
Happy coding!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.