Repository navigation
Hydration mismatch caused by browser extension injecting DOM before React hydration #95632
Replies: 2 comments
|
Hi, Yes, as far as React is concerned, and this is not a Next.js thing necessarily, if the DOM that the server sent is not the same it encounters as it hydrates (to attach event listeners), then we have a problem, a safe way out is to render again, fresh in the client. You can suppress these at DOM element level: That'll also make it respect whatever it finds, but it could cause errors with event handlers, IIRC. This used to happen a lot with Browser extensions that auto apply Dark Mode, or that fill in input fields ahead of time. React 19 wanted to soften this, https://react.dev/blog/2024/12/05/react-19#compatibility-with-third-party-scripts-and-extensions, but I think it is still somewhat out-there. One quick way to cross check this is to use Incognito Mode, or another browser, I typically have Firefox, Edge and Safari to verify various other things. In this case though, your extension is trying to aggressively highlight text nodes, to label them. I think you'd need to assess how much this affects your user base experience. |
|
This is a known class of issue with browser extensions that inject attributes into the DOM. The A few ways to handle it: 1. Suppress hydration warnings on specific elements (quickest fix): <img src="..." suppressHydrationWarning />This tells React to skip the diff check for that element. Safe because the extension attributes don't affect your app behavior. 2. Use const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return null;3. Wrap in a layout effect to defer rendering: useLayoutEffect(() => {
// DOM is clean at this point
}, []);Option 1 is the most practical. For a global fix, you can also add this to your root layout: <html suppressHydrationWarning>But that's heavy-handed. Better to target the specific elements that extensions tend to pollute (images, links, text containers). |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I'm seeing a hydration mismatch warning in a Next.js application, but after inspecting the React diff, it appears the mismatch is caused by a browser extension rather than my application code.
Environment
Error
React diff
The component itself is simply rendering a
next/image:I don't use:
windowlocalStorageDate.now()Math.random()or any client-only branching during the initial render.
The extra attribute (
data-ai-detector-processed) and the injected<div>do not exist in my source code, which makes me think they're being added by a browser extension before React hydrates.Questions
The application works correctly, but I'm trying to determine whether this is expected behavior or whether there's something I should investigate further.
Thanks!
All reactions