Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Next.js hydration error stopping tags: how to find and fix it

TRK-391High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

Next.js and React render your page on the server first, as plain HTML. In the browser, React then "hydrates" that HTML: it attaches click handlers and state so the page becomes interactive. Hydration expects the browser render to match the server HTML exactly.

When they differ, React logs a hydration error. Typical messages are Hydration failed because the initial UI does not match what was rendered on the server and Text content does not match server-rendered HTML. In the worst case React throws away the server HTML and re-renders the page on the client, or part of the page stays dead.

Why it costs you leads or sales

Ad platforms optimise on the conversions you report. If a hydration error blocks the purchase or lead event on some pages or browsers, those conversions go missing. Smart bidding learns from less data, reports look worse than reality, and you may cut campaigns that actually work.

Where a hydration error breaks tracking
  1. Server sends HTMLPage looks fine
  2. React hydratesBrowser render differs from server HTML
  3. Handlers and tags mountListeners and tag code never attach
  4. Conversion hit sentNo event reaches GA4, Ads or Meta

The server HTML loads, but hydration fails and later steps never run.

Conversions reported vs realExample numbers
Conversions reported vs real (example)Real conversions: 100 conversions per week; Reported before fix: 62 conversions per week; Reported after fix: 98 conversions per week038771006298Real conversionsReported before fixReported after fixconversions per week

Example: illustrative numbers for a page where hydration fails for part of its visitors.

How to check if your site has it

Check for hydration errors yourself
  1. Open the page in development mode

    Run next dev and open the page. Next.js shows an error overlay that names the mismatch and often shows a diff of the server and client HTML.

  2. Check the production console

    Open your live page, press F12 and look at the Console tab. Production builds give shorter errors such as Minified React error #418, #423 or #425. Reload with the cache cleared and test on mobile width too.

  3. Watch the Network tab

    Filter for collect, g/collect, googleads, facebook.com/tr or analytics. Load the page and trigger the conversion. If the console shows an error and no hit appears, the two are likely linked.

  4. Test the key journeys

    Do this for the form submit, add to cart and checkout pages, not only the home page. Errors often appear only on pages with dynamic content.

How to fix it

Most hydration errors come from a small set of causes. Find which one applies, then fix it.

Common causes of hydration errors and their fixes
CauseExampleFix
Values that change per renderDate.now(), Math.random(), new Date().toLocaleString() in JSXCompute in useEffect or pass a fixed value from the server
Browser-only checks in rendertypeof window !== 'undefined' choosing different markupRender the same markup first, then update in useEffect
Invalid HTML nesting<div> inside <p>, <a> inside <a>Correct the nesting
Browser extensions or scripts changing the DOMTag or consent scripts editing the body before hydrationLoad them with next/script and avoid editing React-owned nodes
Manually pasted tag snippetsRaw <script> in a component or in <head> edited by handUse the Script component
Fix a client-only value that causes a mismatchjavascript
import { useEffect, useState } from 'react';

export default function Greeting() {
  const [time, setTime] = useState(null);

  useEffect(() => {
    setTime(new Date().toLocaleTimeString());
  }, []);

  // Same output on server and first client render
  return <p>{time ? `Local time: ${time}` : 'Loading...'}</p>;
}
Load tags safely, by setup
  1. Use next/script for tags

    In the App Router, add the GA4 or GTM script in app/layout.js with <Script src="..." strategy="afterInteractive" />. In the Pages Router, put it in pages/_app.js. Do not paste raw script tags inside page components.

  2. Fire events after mount

    Send conversion events from onClick, form handlers or a useEffect, never during render. Check that window.gtag or window.dataLayer exists before calling it.

  3. Fix every error in the console

    Work through each hydration message. One error can hide others, so reload and recheck after each fix.

Load GA4 with next/script (App Router, app/layout.js)javascript
import Script from 'next/script';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"
          strategy="afterInteractive"
        />
        <Script id="ga4-init" strategy="afterInteractive">
          {`window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());
          gtag('config', 'G-XXXXXXXXXX');`}
        </Script>
      </body>
    </html>
  );
}

How to confirm it's fixed

Confirmation checks
  • The browser console shows no hydration errors on the key pages
  • Reload with cache cleared and test on desktop and mobile width
  • The Network tab shows the page_view hit on load
  • The conversion hit appears when you submit the form or reach the thank-you page
  • GA4 DebugView or the Meta Pixel Helper shows the event
  • Test again on the production build, not only next dev

How to prevent it happening again

Habits that keep hydration clean
  • Run next build and test the production site before each release
  • Never use random or time-based values directly in render
  • Load all third-party tags with next/script
  • Treat console errors in code review as release blockers
  • Recheck the key journeys after every deploy, theme change or consent tool update
How the problem often unfolds
  1. Day 1

    A developer adds a date or a banner that renders differently on the server and the client.

  2. Day 1

    The page looks fine, so the release goes out.

  3. Day 3

    Reported conversions in ad platforms drop. Nobody links it to the deploy.

  4. Day 10

    The console error is found and fixed, and hits return.

How FunnelGuard handles this

FunnelGuard detects this today. A robot visitor runs your real journeys on a schedule, from the EU or the US, and watches for console errors and missing tracking hits at each step. When a page logs errors and an expected GA4, Google Ads, Meta or other hit does not arrive, that is flagged.

Questions people ask

Why are my tags not firing in Next.js?

A common cause is a hydration error, where the server HTML and the browser render do not match. Other causes are tag scripts pasted in the wrong place, events fired during render, or consent tools blocking them. Check the browser console and the Network tab first.

What is a hydration error in Next.js?

It is an error logged when React cannot attach to the server-rendered HTML because the browser render differs from it. Messages include Hydration failed because the initial UI does not match and Text content does not match server-rendered HTML. React may then re-render the page or leave parts non-interactive.

Can a hydration error stop Google Analytics or Meta Pixel from working?

Yes, it can. If React discards parts of the page or fails to attach handlers, tag code and event listeners that depend on them may never run. The page can look normal while conversion hits are missing.

How do I fix Text content does not match server-rendered HTML?

Find what renders differently on the server and client, such as Date.now(), Math.random(), locale formatting or a window check. Render the same output first, then set the client-only value in useEffect. Also check for invalid HTML nesting.

Should I use suppressHydrationWarning?

Only for small, harmless differences like a timestamp on a single element. It hides the warning but does not fix a mismatch. Using it widely can leave real tracking problems undetected.

How do I add Google Analytics to Next.js correctly?

Use the next/script component in your root layout (app/layout.js) or pages/_app.js with strategy="afterInteractive". Load the gtag script and run the config call there. Avoid pasting raw script tags inside page components.

How can I tell if a hydration error is hurting my conversions?

Open the console on your key pages and look for hydration errors, then compare with the Network tab for missing hits. A monitoring service that runs your journeys on a schedule can also catch console errors alongside missing hits after a deploy.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: console errors + missing hits.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.