Vibe-Code Rescue

Text content does not match server-rendered HTML

Why it happens

Next.js renders your page to HTML on the server. React then rebuilds the page in the browser and compares it with that HTML. The two have to match, and here they did not.

Code that reads the current time, window or localStorage while rendering gives a different answer on the server than in the browser. Invalid HTML nesting, browser extensions and CDN settings that rewrite the HTML can do the same.

How to fix it

  1. Read the full error in the browser console. It names the element that differs. Start there.
  2. Check your HTML nesting. A p inside another p, a div inside a p, or a ul inside a p is invalid. So is a link inside a link or a button inside a button. Change the tags.
  3. Look for values that change between server and browser: new Date(), Math.random(), window, localStorage, or a check like typeof window !== 'undefined' inside your render code.
  4. Move those values into an effect, so the first render matches the server and the browser fills in the real value after it loads.
    import { useState, useEffect } from 'react'
    
    export default function App() {
      const [isClient, setIsClient] = useState(false)
      useEffect(() => { setIsClient(true) }, [])
      return <h1>{isClient ? 'This is never prerendered' : 'Prerendered'}</h1>
    }
  5. If one component only makes sense in the browser, turn off server rendering for it. This option only works inside a Client Component, so the file must start with 'use client'.
    'use client'
    import dynamic from 'next/dynamic'
    
    const NoSSR = dynamic(() => import('../components/no-ssr'), { ssr: false })
  6. For a value that will always differ, such as a timestamp, add suppressHydrationWarning to that one element. It works one level deep and the docs call it an escape hatch.
    <time dateTime="2016-10-25" suppressHydrationWarning />

When it is not this

Questions

What is a hydration error in Next.js?

It means the HTML rendered on the server differs from what React rendered in the browser on first load. Dates, window, localStorage and invalid HTML nesting are the usual causes.

Is suppressHydrationWarning a real fix?

Only for content that must differ, like a timestamp. The Next.js docs call it an escape hatch that works one level deep. Fix the real difference first.

Sources

Still broken?

The free health check asks ten questions about your app and shows the three fixes to do first. It runs in your browser.

If you would rather have someone read the code, the fixed-price audit is $497 one-time. It covers security and scaling, with a written report ranked by severity.