Hydration failed because the server rendered HTML didn't match the client. As a result…
- User
Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:
- A server/client branch
if (typeof window !== 'undefined'). - Variable input such as
Date.now()orMath.random()which changes each time it's called. - Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.
It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.
See more info here: https://nextjs.org/docs/messages/react-hydration-error
... <AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}> <HTTPAccessFallbackBoundary notFound={}> <HTTPAccessFallbackErrorBoundary pathname="/" notFound={} ...> <body className="geist_e531dabc-module__QGiZLq__variable geist_mono_68a01160-module__YLcDdW__varia..."
-
cz-shortcut-listen="true" > ... ...
src\app\layout.tsx (27:7) @ RootLayout
25 | return ( 26 |
27 | <body | ^ 28 | className={
${geistSans.variable} ${geistMono.variable} antialiased} 29 | > 30 | {children} Call Stack 17Show 15 ignore-listed frame(s) body (0:0) RootLayout src\app\layout.tsx (27:7)
- A server/client branch
- Assistant
This error message means that the HTML generated on the server didn’t match the HTML generated (or potentially modified) on the client during React’s hydration process. In other words, React detected differences between the server-rendered markup and the markup produced on the client, so it discarded the server’s markup and started rendering the tree from scratch on the client.
Common Causes
-
Dynamic Values Without Synchronization
- Using functions like
Date.now()orMath.random()in your render can produce different outputs on the server and client. - Solution: Compute these values either during the server render and pass them down or generate them only on the client (e.g., inside a
useEffect).
- Using functions like
-
Client-Only Checks in a Shared Component
- Checking for the existence of
window(e.g.,if (typeof window !== 'undefined')) directly within the render might cause parts of the component to render differently between server and client. - Solution: Use hooks like
useEffector Next.js’s dynamic import with{ ssr: false }to ensure that code that depends on client-only objects runs only on the client.
- Checking for the existence of
-
Locale-Specific Date or Number Formatting
- When you are formatting dates or numbers that depend on the user's locale, the output on the server (which might use a default locale) may differ from the output on the client.
- Solution: If possible, defer locale-specific formatting to the client or ensure the server uses the correct locale.
-
External or Asynchronous Data
- If your component renders data that can change between the server render and the time it’s hydrated on the client, the output might not match.
- Solution: Either include a consistent snapshot of this data in the initial HTML or fetch the data on the client after hydration.
-
Incorrect HTML Structure or Unsupported Patterns
- Mismatched or incorrectly nested HTML (or even modifications from browser extensions) may also result in this error.
- Solution: Verify that your markup is valid and that no client-side scripts or extensions are modifying the HTML before React hydrates it.
Strategies to Resolve the Issue
-
Defer Client-Only Logic:
If parts of your component require client-side-only APIs, consider wrapping the logic in a hook like this:import { useEffect, useState } from 'react'; function ClientOnlyComponent() { const [isClient, setIsClient] = useState(false); useEffect(() => { setIsClient(true); }, []); if (!isClient) { // Optionally, return a placeholder for SSR return null; } // Now it's safe to use client-only code (e.g., window, Date.now, etc.) return <div>{`Current time: ${new Date().toLocaleTimeString()}`}</div>; } -
Ensure Consistency:
If you need to render dynamic content both on the server and on the client, try to compute values in a way that guarantees the same result on both sides or pass the required data as props from your server-side logic. -
Review Your Layout/HTML:
The error points to the<body>tag in your layout (e.g.,src/app/layout.tsx). Double-check that your HTML structure, including any conditional logic, doesn’t inadvertently cause variations between the server and client output. -
Browser Extensions:
Although less common, browser extensions (like those modifying the DOM) might also cause mismatches. Try disabling them temporarily to determine if they are the source. -
Consult the Documentation:
The error message includes a link to the Next.js documentation on hydration errors which contains more details and may provide additional insight.
Summary
The key is to ensure that the rendered output is the same both during server-side rendering (SSR) and when the component hydrates on the client. By isolating client-only logic (using hooks like
useEffector dynamic imports) and ensuring that any dynamic values are either consistent or deferred to the client, you can eliminate the mismatch and avoid the hydration error.If you follow these strategies and review the components that manage dynamic content carefully, you should be able to resolve the hydration issues in your application.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.