How to Fix Next.js Hydration Failed Error: Text content does not match server-rendered HTML
Next.js App Router or Pages Router detected a mismatch between the pre-rendered HTML sent from the Node/Edge server and the React virtual DOM tree mounted in the browser on initial hydration.
Error: Hydration failed because the initial UI does not match what was rendered on the server
Quick Fix Checklist
- Use a mounted state flag (useEffect(() => setMounted(true), [])) before accessing browser-only data
- Use dynamic import with { ssr: false } for components that rely exclusively on client-only hardware or window APIs
- Format timestamps in a fixed UTC ISO format or pass them from server props
- Add suppressHydrationWarning={true} to specific elements like timestamp spans where minor differences are acceptable