What is Incremental Static (Re)Generation?
Introduced by developers of Next.js, ISR is “updating static content after you have already built your site”. One can understand Incremental Static (Re)Generation, in two parts: Incremental Static Generation and Regeneration of web pages.
For understanding Incremental Static Generation, let the explanation be guided by a scenario where a page requested by the user was not pre-rendered while statically exporting the website. In such a case, a fallback page is served while the data loads. Further, upon completion of building the page with the fetched data, the page is then cached onto the server. Any user requesting the page then would skip over the fallback and loading stages, and directly see the built page. Superb!
Regeneration is inspired by the concept of stale-while-revalidate, where stale data is refreshed at regular intervals of ‘revalidate’ seconds. For pages that have staleWhileRevalidateSeconds value configured, they would be re-built after regular intervals of the configured value.
This can be followed up by two questions:
- Does that allow the pre-rendered pages to be regenerated during runtime? Yes, the pre-rendered pages when deployed would also be ‘revalidated’ after intervals of staleWhileRevalidateSeconds seconds.
- Can new dynamic pages be generated during runtime without using SSR? While there can be client side rendering for the pages that are being built at runtime (i.e. serving the fallback), avoiding SSR altogether for ISG seems unfeasible.
The benefits of ISG and ISR
With Incremental Static Generation and Regeneration, creates the possibility to initially have the minimal static export, and then create and regenerate the pages on-demand, without re-building the website. This would account to faster deployments, hassle-free handling of regenerating the pages without the need to re-build the website.
The drawbacks of ISG and ISR
As the concept is inspired by stale-while-revalidate, the data is certain to be stale for a period. This nature might be undesired for a web product that aims to serve fresh content for each user. Using ISG or ISR would lead to serving the content that is yet to be updated for some set of the users (those who hit it before revalidation time), or might show the fallback for some (those who hit it after maxAgeSeconds time). This might lead to degraded user experience, but that is very specific to the use case.
Incremental Static (Re)Generation with Nuxt.js and Layer0
To configure Nuxt.js apps to have IS(R)G follow the steps below:
Getting started: Create a nuxt app by the following command:
For Choose custom server framework select None
- For Choose rendering mode select Universal (SSR)
In case you already have a nuxt app, just maintain the above configurations and would be good to go.
0. Add the Layer0 CLI via:
1. In nuxt.config.js, add "@layer0/nuxt/module" to buildModules:
3. For IS(R)G, use routes.js (Created automatically by layer0 init command) and add route for the dynamic pages `/some-route/_slug.vue` as following:
4. To test locally how the app would do in production, run the following:
5. To deploy run:
6. Celebrate! 🎉
Enabling ISG via Layer0 with other frameworks
With Layer0’s magical goodies on the top, after running layer0 init, Incremental Static Generation can be enabled with any framework by just adding the following to routes.js (below, the on-demand dynamic page is being served at `/item/:id`):
Example Of Incremental Static Regeneration with Layer0:
With Layer0, it’s easier than ever to implement Incremental Static Regeneration for different purposes with different frameworks. The following seeks to implement ISR with Nuxt.js via Layer0.
Website: Static Medium
Techniques that are being implemented in the example above:
- Defining stale-while-revalidate seconds and max age of the cached pages in routes.js for revalidating the cache: https://github.com/rishi-raj-jain/static-medium-isr-in-nuxtjs-with-layer0/blob/master/app.html#L6
- Loading the google fonts in the best way: To do this, create app.html in the root directory of nuxt project, and follow https://github.com/rishi-raj-jain/static-medium-isr-in-nuxtjs-with-layer0/blob/master/app.html#L6
- Configuring TailwindCSS for purgeCSS: as per the nuxt/tailwind docs
- Search Engine Optimization and Preview Cards for unbuilt pages: https://github.com/rishi-raj-jain/static-medium-isr-in-nuxtjs-with-layer0/blob/master/pages/_slug.vue#L30
- Performance Evaluation: https://gtmetrix.com/reports/rishi-raj-jain-try-default.layer0.link/FzTQEck2/ [Link tested is generated via ISR]