Understand Next.js's incremental static regeneration against the SSR & SSG
Storyblok is the first headless CMS that works for developers & marketers alike.
In this article, you'll be able to learn the difference between Server Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR), and how to implement ISR into your Storyblok projects.
If you prefer to watch the talk instead of reading the article, check the recording of Arisa's talk at Next.js Conf 2021.
This blog post content is presented at Next.js Conf 2021. The talk video will be accessible after the conference.
Advantage of going headless for such a huge project
Before we talk about how to integrate Incremental Static Regeneration (ISR) into your Storyblok projects, let's talk about why we should go for a headless approach with ISR.
Each project could be various scales and bigger scale projects tend to have more challenges to solve. If your projects are distributing their markets on a global scale, the amount of the content tends to be bigger.
One of the common concerns for such cases is building time.
The build time will increase, followed by the number of pages. The time you need to wait could be from minutes to hours. This is a huge blocker when you need to make changes to something in your content. If there are many editors making changes at the same time, it'll be quite hard to handle all the changes in a queue in the right order.
To solve the long build time, frameworks have great selections of choices to solve this issue. In order to be able to choose the best frameworks for each project, there should be flexibility to choose the right tech stack.
The headless approach gives us the flexibility to choose frameworks, libraries, and even languages to enhance performance. Also, it's one of the popular ways to work with modern frameworks.
Going headless architecture brings you other benefits. For instance, Storyblok allows users to use a component-based approach, visual editor, advanced role permissions, and many more.
Pros and cons between SSR, SSG, and ISR
We saw the advantages of going headless approach. Once your projects are set for a headless approach, it's time to decide on the best tech stack. In order to be able to pick the best technology to have the content available as fast as possible, let's see the comparison between SSR, SSG, and ISR.
Server Side Rendering (SSR)
Pros: Pages are always updated whenever being reloaded. Hence, it's quick to have initial access to pages. Good for SEO.
Cons: Performance will be low and require dealing with caching issues. SSR, by default, is often slower than SPA and SSG. Also, more API calls are required because the page HTML is generated in each request.
Static Site Generation (SSG)
Pros: Fast performance and the security attack on a static website can be minimized. Fewer API calls.
Cons: Pages are generated at build time. It means until the next build, page content will stay the same. Longer build time with more pages.
Incremental Static Regeneration (ISR)
Pros: Possible to select the number of pages to generate with an adjustable stale time with
revalidate. Really fast performance with less build time.
Cons: There's still stale time in the beginning.
Server Side Rendering (SSR) was popular in a monolithic world. In a Jamstack world, Static Site Generation (SSG) was often applied. Yet, both approaches had concerns, such as performance, caching, and long build time.
The whole concept is to enable static generation on a per-page basis without rebuilding the entire site.
There's an option to choose the number of pages to generate, so that it keeps the advantages from static-generation, and minimizes the build time.
If there're a lot of pages on your project, ISR enables you to load only the most recent pages at build-time and more pages are cached ahead of a user's request.
The diagram above is describing the loading. In logic, ISR works as the diagram below explains.
Let's say, there are two users visiting a common page, user A and user B.
When user A visits a page at 0 seconds from the diagram, user A sees the stale version of the page. When user B visits the same page after 60 seconds at the lower row, user B sees the updated version of the page.
ISR triggers to generate a page when user A visits the stale version of the page. That's why user B who visited after 60 seconds was able to see the updated version of the page.
Needless to say, it's possible to change the time length by developers. Based on what we took a look at together how ISR from Next.js works in logic, let's move on to see how we can integrate it into a source code level.
We recommend you take a look at our 5 minutes Next.js integration tutorial to have a better understanding of how ISR works at Storyblok projects.
Create [[...slug]].js file
At Next.js, the root or home entry will be created in
pages directory. Create
If you would like to compare SSR, SSG, and ISR in one repository, there is a way to create each directory for all three systems. You can take a look at the
[src/pages directory](https://github.com/storyblok/storyblok-nextjs-multilanguage-isr-ssg-ssr/tree/main/pages) to see that visually in our GitHub repository.
At line 20 and line 47, there are
getStaticPaths Next.js APIs. If you're already familiar with how to set up SSG in your Next.js projects, those Next.js APIs look very similar to SSG setup.
There are two more lines we'd like to take a look at in these Next.js APIs. In the scope of
getStaticProps (line 43), there's an object property,
revalidate with a number value.
revalidate enables static content to be updated dynamically. In this case, it'll keep the stale content within 10 seconds but it'll update content after these 10 seconds expires.
At line 69 in the scope of
fallback is returned. In SSG, the value is either
false to show 404 page as a fallback.) There's also a value of
'blocking'. Yet, 'blocking' will wait for the HTML to be generated and it'll be cached for future requests to keep caching once per path. It's an ideal value for SSR.
In Incremental Static Regeneration (ISR), we set the
fallback value as
'blocking' to have hybrid advantages from SSR and SSG.
Now, we are enabled to use static-generation on a per-page basis, without rebuilding the entire site.
If returning a static page with a loading state on the first request is preferred in your projects,
fallback: true could be set up in ISR. However,
'blocking' is still a preferred value.
In this blog post, we took a look at the difference between Server Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR) from the logical perspective and in an action.
If you'd like to watch the details in a video, we have a talk at Next.js Conf 2021. The video will be accessible after the conference.
|Server Side Rendering (SSR)
|Static Site Generation (SSG)
|Incremental Static Regeneration (ISR)
|GitHub repository demo