Hydration
Hydration is the process where client-side JavaScript takes over server-rendered HTML to make it interactive. The server sends complete HTML, then JavaScript "hydrates" it by attaching event listeners, initializing state, and connecting to the framework's reactivity system.
Traditional hydration re-renders the entire component tree on the client side, matching it against the server HTML. This can be expensive — the browser downloads and executes JavaScript for all components, even those that don't need interactivity.
Modern approaches optimize hydration. Partial hydration (used by Astro) only hydrates interactive components ("islands"), leaving static content as plain HTML. Progressive hydration defers hydrating off-screen components until they're needed. Resumability (Qwik's approach) serializes the application state so the client can resume without re-executing initialization code.
Astro's "islands architecture" is particularly efficient — it sends zero JavaScript by default and only hydrates components marked with client:load, client:visible, or client:idle directives.
How it works
web.dev's "Rendering on the Web" defines hydration as running client-side scripts to add application state and interactivity to server-rendered HTML. The HTML arrives first and can be painted immediately; the JavaScript arrives later and takes over.
The framework re-runs the page's components in the browser, matches their output against the existing DOM, and attaches event listeners and state to the nodes already there instead of recreating them. React's hydrateRoot, for example, attaches React to HTML that was rendered by react-dom/server.
The two renders must agree. React's documentation says the tree passed to hydrateRoot must produce the same output as it did on the server and that mismatches should be treated as bugs: at best they cause a slowdown, at worst event handlers attach to the wrong elements. Typical causes are values that differ between server and browser, such as the current time or random numbers.
Hydration costs main-thread time, and web.dev notes it can significantly hurt Total Blocking Time and Interaction to Next Paint: the page looks interactive before it is. Islands architecture, as in Astro, renders the page to HTML with no JavaScript by default and hydrates only the components marked with directives such as client:load, client:idle or client:visible.
Example
A React component that causes a hydration mismatch, and the usual fix.
// Mismatch: the server and the browser render different times.
function Clock() {
return <p>Rendered at {new Date().toLocaleTimeString()}</p>;
}
// Fix: render the same placeholder in both places, update after hydration.
function Clock() {
const [time, setTime] = useState(null);
useEffect(() => setTime(new Date().toLocaleTimeString()), []);
return <p>Rendered at {time ?? '…'}</p>;
}---
import Counter from '../components/Counter.jsx';
---
<article>Static text: ships as plain HTML, never hydrated.</article>
<Counter client:visible /> <!-- hydrated only when scrolled into view --> In the fixed Clock, the server and the first browser render both output the placeholder, so hydration matches; the effect then runs only in the browser and fills in the time. In the Astro page, only the Counter island downloads and runs JavaScript.
Common confusion
What is the difference between hydration and server-side rendering?
Server-side rendering produces the HTML. Hydration happens afterwards in the browser and makes that HTML interactive. A page can be server-rendered without ever being hydrated, if it has no interactive components, and hydration also applies to statically generated HTML.
What is the difference between hydration and client-side rendering?
Client-side rendering builds the DOM from scratch in the browser, starting from a mostly empty HTML shell, so nothing meaningful is visible until the JavaScript runs. Hydration reuses HTML that already arrived from the server and only attaches behaviour to it, so content is visible earlier even though the JavaScript cost may be similar.
Related tools & demos
- Server-Side Rendering (SSR) Where hydrated HTML usually comes from.
- Static Site Generation (SSG) Build-time HTML that can be hydrated the same way.
Further reading
- Rendering on the Web (Addy Osmani, Jason Miller) web.dev
- hydrateRoot React documentation
- Islands architecture Astro documentation
Want to learn more?
Explore more developer terms or read in-depth articles on the blog.
Browse all terms