Frontend Case Studies

Topic F12

CSR vs SSR vs SSG

Who builds the HTML and when: the browser, the server, or the build.

10 min · intermediate

Watch: compare when the page's content first appears in all three. Who builds the page decides how soon you see it.

servernetworkbrowser0s0.2s0.4s0.6s0.8s1s
  • finished / the result
Step 1 of 7

Client-side rendering: the server sends an almost empty HTML page. The browser downloads the JavaScript, runs it, and then that JavaScript fetches data and draws the page.

The timings are illustrative, but the order is real: who builds the HTML decides when the user sees anything.

How they compare

CSRSSRSSG
First content visibleSlowestFastFastest
Data freshnessLatestLatestAs old as the last build
Server work per requestAPI onlyA full renderNone (static files)
SEO and link previewsWeakGoodGood
Good fordashboards behind a loginpersonalised pages, fast-changing datadocs, blogs, landing pages, this site

Hydration

With SSR and SSG, the HTML arrives complete but not yet interactive. The JavaScript still has to download and React has to attach event handlers to the elements already there. That process is called hydration. Between content appearing and hydration finishing, the page looks ready but its buttons don't respond yet. A small bundle (previous topic) shortens that gap. React Server Components cut hydration work by sending no JavaScript for components that aren't interactive.

Code, for programmersAll three in Next.js (App Router)
// app/docs/page.tsx: SSG, built once at build time (the default when nothing is dynamic)
export default async function Docs() {
  const page = await getDoc("intro");
  return <Article page={page} />;
}

// app/blog/page.tsx: ISR, static but rebuilt in the background at most every 60 s
export const revalidate = 60;

// app/account/page.tsx: SSR, rendered on every request (it reads cookies)
export default async function Account() {
  const session = (await cookies()).get("session");
  return <Profile user={await getUser(session)} />;
}

// components/LiveChart.tsx: CSR, a client component that fetches in the browser
"use client";
export function LiveChart() { const { data } = useQuery({ queryKey: ["live"], queryFn }); /* ... */ }

Quiz

  1. Why does a CSR page stay blank longer?

  2. A documentation page that changes once a week. The best choice?

  3. What is hydration?

0 of 3 answered.