Static Site On-Demand Regeneration
What happens when your static site grows to hundreds, thousands, or even millions of pages? The time that it takes to generate that many pages grows significantly to the point that it cannot be done on every publish (unless you can tolerate many minutes or even hours of delay between the time of publish start and end).
Enter On-Demand Regeneration
To avoid regenerating an entire site every time something is published, you can generate a page when it is requested the first time, after which it will be stored (in whatever backend storage your site uses) and cached (assuming your static site uses a CDN or other cache mechanism).
How would this work?
One way to make this work is to run an edge function that checks for the presence of the file at the requested path. If the file is found, the function ends and the request flow returns to the CDN to return the content. If the file is not found, the function generates the static file, returning the content and storing the file.
Depending on your infrastructure, this could be done in several different ways. It fundamentally requires a way to execute your static site generator in the request flow, which is not normally when a static site generator runs. This requires several key components:
- A (quick) way to execute the static site generator at request time (e.g. Lambda@edge Function, Netlify Edge Functions, etc.)
- A static site generator that can generate a single page at a time based on its path.
- A way to store the generated file in the normal storage for your static site.
- A way to update the cache with the generated file.
- A way to prevent the generator from running when the content is already generated.
- Optionally, a way to regenerate any given page on-demand.