← Back to Frontend, page 2

frontend-0024 · Medium mid

What is static site generation, and how does incremental static regeneration (ISR) keep static pages fresh?

SSG builds HTML ahead of time and serves it from a CDN, which is fast but only as fresh as the last build. ISR keeps the pages static but rebuilds a page in the background after a set time, so the next visitor gets a newer copy without a full site rebuild.

Full answer

Static site generation (SSG) renders pages at build time. The output is plain HTML files, which any CDN can serve with very low latency and no server work per request. It is a good fit for docs, blogs and marketing pages. The weak point is freshness: a changed price or a new article appears only after the next build, and a site with 100,000 pages can take a long time to rebuild.

Incremental static regeneration (ISR) is a common answer in frameworks such as Next.js. The page stays static, with a revalidation time, for example 60 seconds.

  • Within the time, every visitor gets the stored page.
  • After the time, the next visitor still gets the stored (stale) page. At the same moment, the server regenerates the page in the background.
  • When regeneration ends, the stored page is replaced, and later visitors get the new one.
  • Pages that were not built yet can be generated on the first request and then stored.
  • Many frameworks also allow on-demand revalidation, so a CMS webhook can refresh one page at once.

This is the same idea as the HTTP directive stale-while-revalidate, applied to a page store. The trade-off is that one visitor can see a copy that is older than the revalidation time, and the freshness is "eventually".

Do not use SSG or ISR for pages that differ for each user. Fetch personal parts in the browser after load, or use SSR for those routes.

The script below models the idea with a fake clock. The first request builds the page. A request after 30 seconds is served from the store. A request after 61 seconds gets the stale page and starts a rebuild. The next request gets the new page. This is a small model, not the code of a framework.

javascript
function createIsr(render, revalidateMs, now) {
  let page = null, builtAt = 0, pending = null;
  return {
    async get() {
      if (!page) { page = await render(); builtAt = now(); return { html: page, status: 'MISS' }; }
      if (now() - builtAt < revalidateMs) return { html: page, status: 'HIT' };
      if (!pending) {
        pending = render().then((html) => { page = html; builtAt = now(); pending = null; });
      }
      return { html: page, status: 'STALE', done: pending };
    },
  };
}

let clock = 0, version = 1;
const isr = createIsr(async () => `<p>price v${version}</p>`, 60_000, () => clock);
async function visit() {
  const r = await isr.get();
  console.log(`${clock / 1000}s ${r.status} ${r.html}`);
  return r;
}

await visit();               // first build
version = 2; clock = 30_000;
await visit();               // still fresh, old content
clock = 61_000;
const stale = await visit(); // stale copy is served, rebuild starts
await stale.done;
clock = 62_000;
await visit();               // new content

Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node and the output was identical in two runs. The code is a small model of the idea with a fake clock. It is not the implementation of any framework, and no CDN or build was run.

Key points

  • SSG renders HTML at build time and serves it from a CDN.
  • Its weakness is freshness and long builds for many pages.
  • ISR serves a stored page, then rebuilds it in the background after the time is over.
  • On-demand revalidation refreshes a page when the content changes.
  • Personalised content does not belong in a static page.

Common mistakes

  • Thinking ISR removes the stale page: one visitor still gets the old copy.
  • Using SSG for pages that depend on the logged-in user.
  • Setting a revalidation time of a few seconds, which acts like SSR with extra complexity.
  • Forgetting that a CDN in front can add its own caching.

Follow-ups

  • How does on-demand revalidation differ from a time-based one?
  • What happens if the regeneration fails?
  • How would you show prices that must always be current?

Tips

  • Describe the three states: first build, hit and stale.
  • Relate ISR to stale-while-revalidate to show that it is a known pattern.