7. Rendering Strategies — Where Should HTML Be Made?
“Every rendering strategy is a decision about who does the cooking, when, and who’s waiting.”
The story: three ways to feed a guest
Section titled “The story: three ways to feed a guest”You want to feed a guest. You have three options.
The meal kit. You hand them a box of raw ingredients and a recipe card. They go home and cook. It’s cheap for you, it’s flexible, and they wait a long time before eating anything — especially if their kitchen is small and their stove is weak. That’s client-side rendering (CSR): ship JavaScript, let the phone build the page.
The buffet. You cook everything in the morning and lay it out. Guests walk in and eat immediately. It’s fast and cheap per guest, but the food is whatever you cooked this morning. That’s static site generation (SSG): build HTML once, serve it to everyone.
À la carte. The guest orders; the kitchen cooks that dish now; it arrives hot and personalized. The guest waits for the kitchen, and the kitchen needs staff for every order. That’s server-side rendering (SSR): build HTML per request.
And then there’s the strange part, the one that makes modern frameworks complicated: in the web’s version of à la carte, the plate arrives looking perfect — but it’s a photograph of food. The guest can see it, but can’t eat it until a chef arrives at the table and re-cooks the same dish from the same recipe so that the fork works. That’s hydration. It’s the hidden cost in most “server-rendered” React apps, and it’s why Priya’s tap went unanswered in the Prologue.
The strategies, precisely
Section titled “The strategies, precisely”CSR — Client-Side Rendering. The server sends a nearly empty HTML document and a JavaScript bundle. The browser downloads, parses and executes the JS, which fetches data and builds the DOM. Classic single-page apps (Create React App era) work this way. Good: simple hosting (static files), rich interactivity, cheap servers. Bad: nothing meaningful appears until JS has downloaded and run and data has been fetched — a chain of waits that is brutal on a slow phone and network. SEO is weaker (Chapter 22).
SSR — Server-Side Rendering. On each request, the server runs your components, fetches data, and sends complete HTML. The browser can paint content as soon as HTML and CSS arrive. Good: fast first content, fresh per-request data, personalization, good SEO. Bad: a server must run per request (cost, cold starts, TTFB includes rendering time), and the page isn’t interactive until hydration finishes.
SSG — Static Site Generation. Run the same rendering at build time, for every page, and deploy the HTML to a CDN. Good: the fastest possible TTFB (edge cache), near-zero cost, nearly unbreakable. Bad: content is as fresh as the last build; pages that number in the millions make builds slow; no per-user personalization in the HTML.
ISR — Incremental Static Regeneration (Next.js’s name; others call it similar things). Static pages with a shelf life: serve the cached HTML, and when it’s older than N seconds, regenerate it in the background for the next visitor. It’s stale-while-revalidate (Chapter 6) applied to rendering. Pages not built at deploy time can be generated on first request and then cached.
Streaming SSR. Instead of waiting for all data before sending any HTML, the server sends the page shell immediately — header, layout, skeletons — and streams in each slow section as its data resolves (using HTTP’s ability to send a response in chunks). With React, <Suspense> boundaries mark where streaming can happen. TTFB and FCP improve dramatically because the slowest query no longer holds the whole page hostage.
Hydration and its cost
Section titled “Hydration and its cost”Here’s the part most React developers have never had explained.
When the browser receives server-rendered HTML, it can paint it. But the HTML is inert: no click handlers, no state. To make it interactive, React must:
- download the JavaScript for every component on the page,
- parse and compile it,
- re-execute every component in the tree, from the root down, to rebuild React’s internal picture of the UI (the virtual DOM / fiber tree),
- walk the existing DOM and attach event listeners, checking that what it computed matches what the server sent.
That’s hydration. It means the work of rendering the page is done twice: once on the server to produce HTML, and once on the client to produce the in-memory tree. On a fast laptop, hydrating a typical page takes tens of milliseconds. On a mid-range Android phone, it can take hundreds of milliseconds to seconds — often as one long task on the main thread, during which taps are ignored (Chapter 11).
This creates the uncanny valley of SSR: the page looks ready, Priya taps “Save,” and nothing happens.
Partial and progressive hydration
Section titled “Partial and progressive hydration”If hydrating everything is expensive, hydrate less, or later:
- Progressive hydration: hydrate components in priority order, or when they scroll into view, or when the user interacts with them. React 18+’s selective hydration helps here: with Suspense boundaries, React hydrates sections independently and will prioritize the section the user just tapped.
- Partial hydration: don’t hydrate static parts at all. A paragraph of text doesn’t need JavaScript.
Islands
Section titled “Islands”Islands architecture (popularized by Astro) inverts the default. The page is static HTML by default — zero JavaScript — with explicit “islands” of interactivity that hydrate independently: a carousel here (client:visible), a search box there (client:idle). The rest of the page never runs any JS. For content sites, this is a superb fit: most of the page is text and images, and JS cost is proportional to interactivity, not page size. (This book is built that way; its interactive demos load only when you scroll to them.)
React Server Components
Section titled “React Server Components”React Server Components (RSC), the foundation of the Next.js App Router, take the islands idea into React’s component model. There are two kinds of components:
- Server Components (the default in the App Router) run only on the server — at build time or request time. They can be
async, read databases and files directly, and use heavy libraries. Their code is never sent to the browser, so they cost zero client JS and are never hydrated. - Client Components (marked
'use client') are classic React: rendered to HTML on the server too, then shipped as JS and hydrated in the browser. Only they can use state, effects and event handlers.
The server renders the tree and produces two things: HTML for the first paint, and the RSC payload — a compact, streamable description of the rendered server component tree, with “holes” where client components go (and references to their JS chunks and their props). The browser uses the payload to build React’s tree without re-running server components, and hydrates only the client components. On client-side navigations, the router fetches just the RSC payload for the new route, not HTML.
Resumability
Section titled “Resumability”Qwik takes a different path: resumability. Instead of re-executing components to rebuild state, the server serializes the application’s state and where each event handler lives into the HTML. The browser loads a tiny loader; when you click something, it fetches just that handler’s code and runs it. No hydration step at all — the app “resumes” where the server left off. The trade-off is a different programming model and many small lazy fetches at interaction time (which must be prefetched well, or the first click waits on the network).
MPA vs. SPA: what a “navigation” means
Section titled “MPA vs. SPA: what a “navigation” means”This distinction sets up Chapter 10’s question of why navigations feel janky.
In a multi-page app (MPA), every link is a real browser navigation. The browser fetches new HTML, tears down the old page, parses and renders the new one. Benefits: simple, memory is reset every page, the browser’s built-in machinery (bfcache, streaming parser, scroll restoration, focus reset, prerendering) works for free. Costs: a full request per page, the old page vanishes before the new one paints (a flash of white unless the browser’s “paint holding” covers it), and historically no transitions.
In a single-page app (SPA), the first load is a real navigation; after that, the router intercepts link clicks, updates the URL with the History API, fetches data (and maybe a route chunk), and re-renders in place. Benefits: shared state survives, transitions are possible, only data moves. Costs: all the work the browser used to do — loading states, scroll restoration, focus management, announcing the new page to screen readers (Chapter 15) — is now your job, and every navigation runs JavaScript on the main thread: download the chunk, parse it, render the new tree, commit it, run effects, lay out an entire new page. That’s where jank lives.
A decision framework
Section titled “A decision framework”There is no best strategy, only a best strategy for a page. Most real apps mix them, route by route. Ask these questions in order:
- Is the content the same for everyone, and does it change rarely? → SSG. Marketing pages, docs, blog posts, essays.
- Same for everyone but changes often (product pages, prices, news)? → ISR / edge-cached SSR with
stale-while-revalidate. - Personalized or per-request (search results, a logged-in feed, cart)? → SSR, streamed, with heavy parts behind Suspense; cache what you can per user segment.
- Behind a login, highly interactive, not indexed (dashboards, editors)? → CSR is fine, even good — but still ship a fast shell, split by route, and consider SSR for the first view if devices are weak.
- Within any of the above, how much is interactive? → Minimize hydrated surface: islands, server components, client components at the leaves.
Applied:
- A content site (Lumen’s essays): SSG, islands for the lightbox and like button. Near-zero JS for readers.
- An e-commerce site: category and product pages ISR/edge-cached (SEO and speed matter; prices change), cart and checkout SSR, personalization streamed in after the cached shell.
- A dashboard: CSR (or SSR shell + client data) behind login, aggressive code-splitting, data caching on the client, and attention to INP because users interact constantly.
When it goes wrong
Section titled “When it goes wrong”Healthcare.gov (2013). The US government’s health-insurance marketplace launched in October 2013 and collapsed almost immediately. Only a handful of people managed to enrol on day one. Among many problems (rushed timelines, dozens of contractors, little end-to-end testing), a notable architectural one: every visitor, even those just browsing plans, was pushed through account creation first, funnelling all traffic into the slowest, most stateful part of the system. Pages that could have been static content were coupled to backends that couldn’t take the load. A “tech surge” team later rebuilt the critical paths, and a simple lesson stuck: serve what can be static as static, and don’t put your heaviest dynamic work in front of your highest traffic. The original budget was several hundred million dollars; the eventual cost much more.
The hydration mismatch. A team ships an SSR page that renders “Posted 5 minutes ago.” The server renders it at 10:00:00 UTC; the client hydrates at 10:00:03 in IST, computes a different string, and React warns of a mismatch — or, worse, in production, patches the text silently or re-renders the subtree, causing a flash and wasted main-thread time. Variants: Math.random() in render, window.innerWidth checks during render, locale-dependent number formatting, and browser extensions (password managers, grammar checkers) injecting attributes into the DOM before React hydrates. Fixes: render time-relative and locale-dependent strings on the client after mount (or pass the server’s timestamp and locale down explicitly), avoid environment checks during render, and use suppressHydrationWarning only for genuinely unavoidable leaf text.
What you now know
Section titled “What you now know”You can define CSR, SSR, SSG, ISR and streaming SSR precisely; explain hydration as “rendering twice” and why it creates the uncanny valley; describe partial hydration, islands, RSC (and its payload) and resumability; contrast MPA and SPA navigations; and choose a strategy per route with a defensible framework. You’re ready for Part II, where we go inside the browser to see what all this HTML and JS actually costs.
Self-quiz
Section titled “Self-quiz”1. Why is hydration “doing the work twice”?
The server renders components to HTML; the client re-executes the same components to rebuild React’s tree and attach handlers.
2. What does a Server Component cost in client JavaScript?
Nothing for its own code; it’s represented in the RSC payload, never shipped or hydrated.
3. What problem does streaming SSR solve?
The slowest data dependency no longer delays the whole HTML; the shell and fast sections are sent immediately.
4. Name two jobs an SPA router takes over from the browser.
Scroll restoration, focus management, loading indicators, announcing navigation to assistive tech, history handling.
5. What’s the ISR equivalent in HTTP caching terms?
stale-while-revalidate: serve cached HTML, regenerate in the background after it expires.
Exercises
Section titled “Exercises”Warm-up 1. Classify the routes. For a news site: homepage, article page, search results, logged-in “saved articles,” admin CMS. Pick a strategy for each.
Solution
Homepage: ISR/edge-cached SSR (short TTL). Article: SSG/ISR. Search: SSR (streamed). Saved: SSR or CSR behind login. CMS: CSR.
Warm-up 2. Find the mismatch. A component renders {new Date().toLocaleTimeString()}. What happens with SSR?
Solution
Server and client times and locales differ → hydration mismatch. Render on the client after mount, or render a stable server value.
Warm-up 3. Boundary placement. A page has a static essay and a “like” button. Where does 'use client' go?
Solution
Only in the LikeButton component file. The page and essay stay server components.
Lab 4. Lumen’s rendering table. Write Lumen’s route table: route, strategy, cache headers, rationale. Implement: essays SSG, /search SSR, /dashboard CSR.
Expected outcome
next build shows ○/● for essays, ƒ for search; the dashboard page is a small server shell with a client component.
Lab 5. Measure hydration. Record a Performance trace of Lumen’s essay page load with 4× CPU throttling. Find the hydration work on the main thread.
What to look for
A long Scripting block after the JS chunks evaluate, with React’s hydrate/render functions in the flame chart. Note its duration; you’ll shrink it.
Lab 6. Push client boundaries down. Find the highest 'use client' in Lumen and move it down to the leaves. Compare First Load JS in the build output before and after.
Expected outcome
Route JS drops; hydration time drops proportionally.
Lab 7. Stream a slow section. Add an artificial 2 s delay to Lumen’s “related essays” data and wrap it in Suspense. Compare TTFB and FCP with and without streaming.
Expected outcome
Without Suspense, TTFB ≈ 2 s. With it, the shell arrives immediately and related essays stream in later.
Staff 8. Defend a choice. Your team wants to rebuild a marketing site as a client-rendered SPA “for consistency with the app.” Write the counter-argument with user and business impact.
Model answer
Marketing pages need fast first content on unknown devices and solid SEO; CSR delays both and adds JS cost for no interactivity benefit. Consistency of developer experience is real but achievable with the same component library under SSG/islands.
Staff 9. The dashboard that feels slow. A PM says the analytics dashboard “feels sluggish” when switching tabs. Outline what you’d investigate, in order.
Model answer
Record the interaction: network wait for data vs. JS render time vs. layout of a big DOM. Likely fixes: prefetch tab data, keep previous tab’s data cached, virtualize big tables, split heavy charts, yield during render. Chapter 10 has the full anatomy.