More Next.js answers

by Danny
Published 3 min read

Why Next.js throws a hydration mismatch when server HTML and the first client render differ, and how to fix dates, random values, browser-only checks and invalid nesting.

The short answer

#

React found that the HTML the server sent is different from what the same component rendered on the first pass in the browser. Older versions worded this as "Text content does not match server-rendered HTML". The fix is to make the first client render produce exactly the same output as the server, and move anything browser-specific into an effect that runs after hydration.


_14
'use client';
_14
_14
import { useEffect, useState } from 'react';
_14
_14
export function LocalTime() {
_14
const [time, setTime] = useState<string | null>(null);
_14
_14
useEffect(() => {
_14
// Runs only in the browser, after hydration
_14
setTime(new Date().toLocaleTimeString());
_14
}, []);
_14
_14
return <span>{time ?? '--:--'}</span>;
_14
}

Why it happens

#

In the App Router, every component renders on the server first, including Client Components. The browser receives that HTML straight away, then React loads and hydrates it: it renders your components again and attaches event handlers to the existing DOM. React expects both renders to match. When they do not, React reports a hydration error and throws away the server HTML for that part of the tree, rendering it again on the client. You lose some of the benefit of server rendering, and users may see a flash.

The usual causes are:

  • Values that change between server and client, such as new Date(), Date.now() or Math.random().
  • Locale and time zone differences. The server might format dates in UTC while the browser uses the user's zone.
  • Checks like typeof window !== 'undefined' that change what the component returns.
  • Reading localStorage, window.innerWidth or cookies in the browser during render.
  • Invalid HTML nesting, such as a div inside a p, or a p inside another p. The browser repairs the HTML before React sees it, so the trees no longer match.
  • Browser extensions that inject attributes or elements into the page before React loads.

How to fix it

#

Render browser-only values after mounting

The pattern in the short answer is the most reliable. Render a stable placeholder on the server and on the first client render, then fill in the real value in useEffect. Effects never run on the server, so both renders match.

Pass the value from the server

If the server can work out the value, compute it once and pass it down as a prop. Both renders then use the same data.


_10
// app/page.tsx (Server Component)
_10
import { Greeting } from './greeting';
_10
_10
export default function Page() {
_10
const renderedAt = new Date().toISOString();
_10
return <Greeting renderedAt={renderedAt} />;
_10
}

For dates, format with an explicit time zone, for example Intl.DateTimeFormat('en-GB', { timeZone: 'Europe/London' }), so the server and browser agree.

Skip server rendering for one component

For a widget that only makes sense in the browser, such as a chart that measures the window, load it with next/dynamic and ssr: false. This option must be used inside a Client Component.


_10
'use client';
_10
_10
import dynamic from 'next/dynamic';
_10
_10
const Chart = dynamic(() => import('./chart'), { ssr: false });
_10
_10
export function Dashboard() {
_10
return <Chart />;
_10
}

Fix invalid nesting

Read the error overlay in development. Next.js shows a diff of the server and client trees and often names the element. If it points at nesting, change the outer p to a div, or move the block element out.

Suppress a mismatch you cannot avoid

For a single element whose text is meant to differ, such as a timestamp, add suppressHydrationWarning to that element. It only works one level deep and should be a last resort, not a blanket fix.


_10
<time suppressHydrationWarning>{new Date().toLocaleTimeString()}</time>

Common pitfalls

#
  • Using typeof window checks to return different JSX. This is a classic source of the error, because the server and the first client render take different branches.
  • Putting suppressHydrationWarning on html or body and assuming it covers the whole page. It does not reach child elements.
  • Testing only in a desktop browser with extensions disabled. Ask users which extensions they run if the error only shows in production logs.
  • Assuming the error is harmless. React recovers, but it re-renders the tree on the client, which costs performance.
#

Was this resource helpful?