Lesson 8 of 9 · 27m
Hydration Is a Cost Center
Understand why server-rendered HTML still costs client CPU, how hydration mismatches and eager interactivity hurt users, and how to reduce or defer the work.
Server rendering can produce beautiful HTML quickly and still deliver a sluggish product. The browser displays markup, users try to click, and nothing happens because React has not finished downloading, parsing, executing, and attaching the application. Hydration is the bill for making server output interactive. It is often worth paying; it is never free.
The uncomfortable sequence
server renders HTML
browser paints meaningful content
JavaScript bundle downloads
browser parses and compiles JavaScript
React builds the component tree
event handlers and effects attach
the page becomes reliably interactiveThat gap matters most on mobile. The visible page is a promise the interface cannot yet keep. If hydration takes four seconds, users experience dead clicks, lost input, jumping focus, and duplicated work.
Hydration repeats render work
React must reconcile server HTML with the client tree. Even when DOM nodes are reused, component functions run, element trees are constructed, handlers attach, and effects execute. Large static pages with tiny interactive regions pay a large tax when the whole page hydrates as one unit.
Islands, selective hydration, resumability, and server components differ mechanically but ask the same question: which parts are truly interactive, and when?
Mismatches are correctness and performance bugs
If server and client render different output, React may discard matching DOM and repair the tree. Common causes include:
<div>{new Date().toLocaleString()}</div> // time differs
<div>{window.localStorage.getItem('theme')}</div> // browser-only render input
<input id={Math.random().toString(36)} /> // unstable idUse deterministic serialization. Render browser-only enhancements after hydration or isolate unavoidable differences narrowly.
Defer noncritical interactivity
Not everything must hydrate immediately. Menus can hydrate on first intent; comments can hydrate when visible; heavy editors can load after interaction:
function DeferredComments() {
const ref = useRef<HTMLDivElement>(null)
const [enabled, setEnabled] = useState(false)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) setEnabled(true)
})
observer.observe(ref.current!)
return () => observer.disconnect()
}, [])
return <div ref={ref}>{enabled ? <CommentsClient /> : <CommentsStatic />}</div>
}This is a design decision. If a control is visible, it should not pretend to be active before it is.
Streaming and priority matter
Streaming rendering and selective hydration can prioritize important boundaries: navigation and the primary form before a lower-priority feed. Priority hints cannot rescue an application that ships every dependency to every visitor.
Measure the dead zone
Instrument the time between meaningful paint and successful interaction. Synthetic tests can click at fixed intervals and observe when behavior becomes reliable. The crude question is honest: when does the page stop lying?
Field notes
- Early HTML can worsen perceived betrayal if controls are visible but inert.
- Browser-only APIs belong in effects or event handlers, not shared render paths.
- Effects run after hydration; they cannot make pre-hydration controls interactive.
- Server rendering is not a performance strategy by itself. It is half of a delivery contract.
Was this lesson helpful?