← Back to React, page 7

react-0124 · Medium mid

Compare CSR, SSR, SSG, ISR and React Server Components. When would you use each?

CSR sends an empty shell and renders in the browser. SSR renders HTML on the server for each request. SSG renders HTML once at build time. ISR serves static HTML and regenerates it in the background after a time or on demand. Server Components render on the server and send a serialized result with no component JavaScript to the client, and can combine with any of the others. Choose by how fresh and personalized the data is, SEO needs and server cost.

Full answer

These terms describe where and when the HTML is produced.

Client-side rendering (CSR). The server returns a nearly empty HTML file and a JavaScript bundle. The browser runs React and fetches data, then draws the UI. Simple hosting (any static host), great for logged-in dashboards, but the first paint is later and search engines see little unless they execute JavaScript.

Server-side rendering (SSR). For each request the server runs React and returns complete HTML, followed by hydration in the browser. Good for SEO and fast first content with personalized or frequently changing data. It costs server compute and a time to first byte that depends on data fetching. Streaming improves it.

Static site generation (SSG). HTML pages are generated at build time and served from a CDN. Fastest and cheapest, ideal for content that changes rarely (docs, marketing, blogs). Content is fixed until the next build.

Incremental static regeneration (ISR). SSG with updates: a page is served from cache and regenerated in the background after a revalidate time or when you trigger it on demand. It scales a catalog of thousands of pages without full rebuilds. The data may be briefly stale.

React Server Components (RSC). A component model, not a delivery strategy by itself. Server Components run only on the server (at build time or per request), can read data directly, and add no JavaScript to the bundle. The client receives a serialized description to merge with Client Components. A framework such as Next.js combines RSC with static, dynamic and cached rendering per route.

Choosing: dashboards behind login: CSR or SSR with RSC. Marketing and docs: SSG. Large catalog with updates: ISR or cached RSC. Personalized, SEO-relevant pages: SSR (streaming). Modern frameworks let you pick per route, and newer versions use "static shell plus dynamic holes" ideas (partial prerendering), so name the trade-offs, not only the acronyms.

The example shows what the browser receives for each approach, and a small cache that regenerates after a time limit.

jsx
import { renderToString } from 'react-dom/server';

function CoursePage({ title, generatedAt }) {
  return (
    <main>
      <h1>{title}</h1>
      <p>Generated at tick {generatedAt}</p>
    </main>
  );
}

// CSR: the server sends an empty shell, and the browser fills it in.
console.log('CSR first HTML: ' + '<div id="root"></div>');

// SSR: HTML is rendered for each request.
let tick = 0;
const ssr = () => renderToString(<CoursePage title="React Basics" generatedAt={++tick} />);
console.log('SSR request 1: ' + ssr());
console.log('SSR request 2: ' + ssr());

// SSG: HTML is rendered once at build time and reused.
const built = renderToString(<CoursePage title="React Basics" generatedAt={++tick} />);
console.log('SSG request 1: ' + built);
console.log('SSG request 2: ' + built);

// ISR: serve the cached page, and regenerate when it is older than the revalidate time.
let cached = { html: built, createdAt: 0 };
function isr(now, revalidate) {
  const stale = now - cached.createdAt >= revalidate;
  const served = cached.html;
  if (stale) cached = { html: renderToString(<CoursePage title="React Basics" generatedAt={++tick} />), createdAt: now };
  return (stale ? 'served stale, regenerated: ' : 'served from cache: ') + served;
}
console.log('ISR at t=5: ' + isr(5, 10));
console.log('ISR at t=12: ' + isr(12, 10));
console.log('ISR at t=13: ' + isr(13, 10));

This ran as a script. The tick counter stands in for "data fetched at that moment". ISR is simulated with a small cache and not a real framework. The SSG page is the same string on every request. Server Components are not shown here, because they need a framework or bundler support. Output of the run: CSR first HTML: <div id="root"></div> SSR request 1: <main><h1>React Basics</h1><p>Generated at tick <!-- -->1</p></main> SSR request 2: <main><h1>React Basics</h1><p>Generated at tick <!-- -->2</p></main> SSG request 1: <main><h1>React Basics</h1><p>Generated at tick <!-- -->3</p></main> SSG request 2: <main><h1>React Basics</h1><p>Generated at tick <!-- -->3</p></main> ISR at t=5: served from cache: <main><h1>React Basics</h1><p>Generated at tick <!-- -->3</p></main> ISR at t=12: served stale, regenerated: <main><h1>React Basics</h1><p>Generated at tick <!-- -->3</p></main> ISR at t=13: served from cache: <main><h1>React Basics</h1><p>Generated at tick <!-- -->4</p></main>

Key points

  • CSR renders in the browser, SSR on each request, SSG at build time, ISR regenerates static pages over time.
  • Server Components add no client JavaScript for server-only parts and combine with the other strategies.
  • CSR fits logged-in apps. SSG fits stable content. SSR fits fresh or personalized SEO pages.
  • ISR trades freshness for scale, with time-based or on-demand regeneration.
  • Frameworks mix strategies per route. Explain trade-offs, not only definitions.

Common mistakes

  • Treating RSC, SSR and SSG as competing options when they combine.
  • Using SSR for content that never changes, paying server cost for nothing.
  • Using CSR for public pages that need search indexing.
  • Forgetting that ISR data can be stale for up to the revalidate time.
  • Naming acronyms without trade-offs.

Follow-ups

  • How does streaming change the cost of SSR?
  • How would you trigger on-demand revalidation after a content edit?
  • Where do Server Components fit in an SSG site?

Tips

  • Define each by "where and when HTML is produced".
  • Say RSC is orthogonal to the others.
  • Pick by data freshness, personalization and SEO.