SSR Hydration: Bringing Static Pages to Life
Hydration attaches JavaScript interactivity to a static HTML page sent from a server. It's used in Server-Side Rendering (SSR) frameworks to make the initial fast-loading page interactive.
WHY IT EXISTS Client-side apps can feel slow, showing a blank screen while JavaScript downloads and runs. Server-Side Rendering (SSR) solves this by sending a complete HTML page first for a fast initial view. But that page is just a static snapshot. Hydration exists to make that static, non-interactive page fully dynamic.
THE MENTAL MODEL Think of SSR as delivering a beautiful, fully assembled mannequin to a store. It looks like a person, but it's lifeless. Hydration is the process of waking that mannequin up, giving it a brain (state), a nervous system (event listeners), and the ability to move (interactivity). The static HTML is the body; hydration attaches the JavaScript soul.
HOW IT WORKS During an SSR process, a server runs your JavaScript framework (like Vue or React) to generate an HTML string of your app's initial state. This HTML is sent to the browser and displayed immediately. In parallel, the browser downloads the client-side JavaScript bundle. Once executed, instead of re-creating the DOM from scratch, the framework scans the existing HTML and attaches the necessary event listeners and state, making the page interactive.
WHEN TO USE IT Use hydration as part of an SSR strategy when fast time-to-content is critical. This is vital for public-facing websites where SEO matters, e-commerce sites where load speed impacts conversion rates, and any application where a good user experience on slow networks or devices is a priority.
WHEN NOT TO USE IT Hydration isn't needed for applications that are purely client-side rendered, like an internal admin dashboard where initial load time is less important than long-term functionality. The added complexity of an SSR build process and server environment is overkill for simple apps or sites where a Static Site Generator (SSG) would suffice.
ONE CANONICAL EXAMPLE A user visits an e-commerce product page built with Nuxt.js. The server sends complete, static HTML, and the page appears instantly. While the user views the product, the client-side JavaScript downloads. Hydration then occurs: the 'Add to Cart' button becomes clickable, the image gallery becomes interactive, and the app is now a fully functional Single-Page Application. A common error, a hydration mismatch, would occur if client-side code (like reading window.innerWidth) produced a different DOM structure than what the server sent.
Read the original → vuejs.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.