ReactMedium

What is SSG (Static Site Generation)?

Static site generation renders pages to HTML at build time, once, and serves the files from a CDN for every request. It gives the fastest possible delivery and cheap hosting for content that does not change per user, such as marketing pages, docs and blogs. Changes require a rebuild, or incremental regeneration in frameworks that support it.

How it works

  1. At build time, the framework runs your components and data loading for each route.
  2. It writes HTML (and data files or RSC payloads) to disk.
  3. A CDN serves them directly; React hydrates for interactivity.

React APIs

React 19's prerender and prerenderToNodeStream (from react-dom/static) wait for all data before producing HTML, which is what static generation needs. React 19.2 added partial prerendering: prerender can return a postponed state that resume completes at request time.

In frameworks

  • Next.js: routes without request-time data are prerendered automatically; generateStaticParams lists dynamic paths; output: "export" produces a fully static site.
  • React Router framework mode: prerender option in react-router.config.ts.
  • Astro, Gatsby: content-focused static generators that can use React components.

Freshness options

  • Rebuild on content changes (CMS webhook triggers a deploy).
  • ISR / revalidation: regenerate pages in the background after a time or on demand (Next.js cacheLife, revalidateTag).
  • Client-side fetching for the few dynamic bits.

Trade-offs

Build time grows with the number of pages; per-user content is impossible without client-side or server-side rendering.

Code example

tsx
1// build-static.tsx - prerender pages to HTML files at build time (React 19)
2import { writeFile, mkdir } from "node:fs/promises";
3import { prerenderToNodeStream } from "react-dom/static";
4import { text } from "node:stream/consumers";
5
6function BlogPost({ title, body }: { title: string; body: string }) {
7 return (
8 <html lang="en">
9 <body>
10 <article>
11 <h1>{title}</h1>
12 <p>{body}</p>
13 </article>
14 </body>
15 </html>
16 );
17}
18
19const posts = [
20 { slug: "hello", title: "Hello world", body: "First post" },
21 { slug: "react-19", title: "What's new in React 19", body: "Actions, use, and more" },
22];
23
24for (const post of posts) {
25 const { prelude } = await prerenderToNodeStream(<BlogPost {...post} />); // waits for all data
26 await mkdir(`dist/blog/${post.slug}`, { recursive: true });
27 await writeFile(`dist/blog/${post.slug}/index.html`, "<!doctype html>" + (await text(prelude)));
28}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app