Frontend / Hydration

Hydration

A server-rendered page shows up fast but cannot do anything yet. Follow one page from first paint to first working click, and see what hydration attaches in between.

Intermediate7 steps~6 min
Server — renders the page to HTML
Page — the HTML painted in the browser
Script — the JavaScript that adds behaviour
Visitor — clicks, sometimes too early
// THE SHORT VERSION

In 30 seconds

A server can send a page as finished HTML, so it appears almost at once. But HTML is only the look. The buttons do nothing until the page’s JavaScript has loaded.

Hydration is that script taking over the HTML already on screen: it checks the elements are the ones it expected and attaches its event handlers and state to them, without rebuilding anything.

Until it finishes, the page looks ready but ignores clicks. Partial and progressive hydration shrink that gap by hydrating only the interactive pieces, most urgent first.

Hydration, step by step

STEPS01 / 07

The timings and file sizes are made up to keep the sums easy. Frameworks differ in detail: React, for instance, listens for events at the root rather than on every button, and can replay some early clicks.

STEP 01 / 07 The server sends real HTML
THE MATH
GET /trip 200 OK text/html painted at 0.4 s
  1. Step 1: The server sends real HTML. The server runs the page’s components and sends the result as finished HTML. The browser can paint it straight away, before any JavaScript has arrived: here the visitor sees the page after 0.4 seconds.
  2. Step 2: It looks ready, but is inert. HTML describes what the page looks like, not what it does. Until the JavaScript has loaded and run, the Like button is only a picture of a button. A click at 1.0 seconds does nothing, which is why this stretch is called the uncanny valley.
  3. Step 3: The code renders again. At 1.8 seconds the script is ready. It runs the same components the server ran and works out, in memory, which elements the page should contain. Then it walks the HTML already on screen and checks that each element is the one it expected.
  4. Step 4: Attach, do not rebuild. Because everything matched, the script keeps the existing elements and only connects its event handlers and state to them. That is hydration: dry HTML gets its behaviour. From 2.4 seconds a click on Like counts. The gap lasted 2.0 seconds.
  5. Step 5: A hydration mismatch. Hydration assumes the browser renders exactly what the server did. A component that prints the current time breaks that: two seconds have passed. The framework warns, then patches the text or discards that part and renders it again, which costs time and can flicker.
  6. Step 6: Partial hydration: islands. Most of this page never changes, so hydrating all of it is wasted work. Partial hydration ships code only for the interactive pieces, called islands. The heading and text stay plain HTML, and the browser loads 30 kB of script instead of 200 kB.
  7. Step 7: Progressive hydration. The remaining islands need not wake up all at once. Progressive hydration does them in order of need: the Like button the visitor is looking at first, the comment form later, when the browser is idle, when it scrolls into view or when it is first touched.
VALUES SO FAR
painted0.4 stime
handlers····inert
script····time
handlers····live
usable····time
gap····cost
mismatch····bug
script····islands
Like····first

Spotted a mistake on this page?