Next.js: What Is Hydration?
An in-depth look at what hydration is in the context of Next.js, with descriptions on how to address common causes of hydration mismatch errors.
Listen to Article
Next.js: What Is Hydration?
Why does hydration exist?
-
Pure Client-Side Rendering (CSR) leads to users staring at a blank screen while JavaScript downloads and executes in the clients browser.
-
Server-Side Rendering (SSR) fixes this by sending pre-rendered HTML/CSS instantly from the server, giving users an improved First Contentful Paint (FCP).
-
This comes with an important caveat though: HTML is inert. Since it is rendered on the server, it cannot tap into browser events (buttons can't handle
onClick, forms aren't connected to state and so on).
- To address this caveat, we use hydration.
What is hydration?
React's process of attaching event handlers to the DOM to make server-rendered static HTML interactive. During hydration, React reconciles the server-rendered markup with the client-side JavaScript.
- Effectively, the idea is that we can make the page render instantly and at the same time maintain client side interactivity by rendering the static shell of the page first and then adding the client side features.
Why is it called hydration?
- The commonly used analogy is of a sponge that is initially dry and is then soaked in water.
How does hydration work under the hood?
- Server phase: React executes components on the server using methods like:
renderToString(traditional / synchronous)renderToReadableStream(modern / streaming)
- Parsing phase: The browser parses the HTML and renders the visual layout immediately.
- Download phase: The browser fetches the JavaScript bundle containing React and your component code.
- Reconciliation and attachment phase: React executes in the browser, builds an in-memory Virtual DOM tree, matches (reconciles) those nodes with the real DOM nodes already on screen, and binds all event handlers without re-rendering the actual HTML elements.
What are hydration mismatch errors?
While rendering your application, there was a difference between the React tree that was prerendered from the server and the React tree that was rendered during the first render in the browser (hydration).
- To put it simply, a hydration mismatch error is a discrepancy between the HTML that was pre-rendered on the server and what React computed on the client.
What causes hydration mismatch errors (common triggers)?
- Browser-only APIs: Accessing
window,localStorage, ordocumentduring the initial render. - Dynamic Data: Rendering
new Date()orMath.random()during render, which produces different values on server vs. client. - Invalid HTML Nesting: Browser auto-correcting illegal markup (e.g., placing a
<div>inside a<p>tag), causing the client DOM to differ from the server HTML string.
How do you fix hydration mismatch errors (common fixes)?
Resolving hydration mismatch errors comes down to one core principle - ensure the initial client render produces the exact same HTML structure and text that was rendered on the server.
- Shift Client-Side State into
useEffect.- As mentioned, trying to access browser only APIs on the initial render (such as
window,localStorage, ordocument) will cause a hydration mismatch. - To resolve it, you can initialise state with a default value, then update it inside
useEffect. - This works because
useEffectruns only in the browser after the initial hydration process.
- As mentioned, trying to access browser only APIs on the initial render (such as
- Handle non-deterministic data properly (dates, timestamps, random numbers).
- The server and client will evaluate non-deterministic data like dates (
new Date()) or random values (Math.random()) differently.
- The server and client will evaluate non-deterministic data like dates (
- There are two possible solutions for this:
- Compute timestamps on the server (in a server component) and pass the formatted string down as a prop.
- Keep the initial markup static and format the date on the client side inside a
useEffectoruseSyncExternalStore.
- Fix invalid HTML nesting.
Use valid HTML structure instead:
- Disable SSR for specific components.
- Sometimes a component will rely heavily on browser-only functionality.
- In this case, you can disable SSR for that particular component which can resolve a hydration mismatch error.
- Use
suppressHydrationWarning.- When text or attributes are unavoidably different between the server and the client, you can use
suppressHydrationWarningto prevent the error from showing for that specific component.
- When text or attributes are unavoidably different between the server and the client, you can use
- Proceed with caution, though! It does not actually fix the structural mismatch but just silences the error. It should be used sparingly.
Stay Connected
Get notified whenever we publish new articles, insights, guides and product updates.