Repository navigation
How to modify dom or insert dom without hydration mismatch issue #94971
Replies: 3 comments 13 replies
I think your understanding is correct: the EdgeWorker is modifying the HTML after Next.js has already produced the server-rendered React tree, but React still hydrates using the tree it expects from the original render.
So from React’s point of view, the server/client tree is still something like:
```tsx
<div
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html: '<akamai:include url="/banner url" />',
}}
/>But the browser DOM at hydration time has become: <div id="banner-slot">
<div class="banner">Dynamic Banner Content</div>
</div>That means React is hydrating a DOM structure it did not render. During hydration/reconciliation, React can remove or replace the injected content because that content is inside a React-owned subtree. I would not expect As far as I know, there is no supported App Router API that marks a region inside the React tree as permanently outside React ownership. If the node is rendered by your React/Next app, React should be treated as owning that node and its children. The safer patterns are: 1. Let Next render the final banner HTMLIf possible, move the personalization decision before the Next render. For example, the EdgeWorker/CDN can set a header or cookie, and the Next.js Server Component can render the correct banner during SSR. Example idea: import { headers } from "next/headers"
export default async function Page() {
const h = await headers()
const bannerVariant = h.get("x-banner-variant")
const bannerHtml = await getBannerHtml(bannerVariant)
return (
<div
id="banner-slot"
dangerouslySetInnerHTML={{ __html: bannerHtml }}
/>
)
}In this approach, the HTML React hydrates is the same HTML React expects. That avoids the mismatch because the banner is part of the actual React server output. 2. Keep the initial React output stable, then inject after hydrationIf the banner must be loaded separately, another option is to render a stable empty slot and fill it on the client after hydration. "use client"
import { useEffect, useRef } from "react"
export function BannerSlot() {
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
async function loadBanner() {
const res = await fetch("/banner-url")
const html = await res.text()
if (ref.current) {
ref.current.innerHTML = html
}
}
loadBanner()
}, [])
return <div id="banner-slot" ref={ref} />
}This avoids the hydration mismatch because React initially renders the same empty The tradeoff is that the banner is no longer fully edge-injected before hydration, and you need to be careful with sanitization/CSP if inserting raw HTML. 3. Put the edge-injected content outside the React rootIf the content must be inserted at the edge before the browser receives the HTML, then the most reliable way is to inject it outside the DOM region that React hydrates. For example, the CDN/EdgeWorker could insert a banner before or after the app root, or into a separate container that React never renders into. Inside the App Router tree, though, this is hard to guarantee because React/Next owns the rendered app structure. What I would avoidI would avoid this pattern: <div
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html: '<akamai:include url="/banner url" />',
}}
/>and then replacing that markup at the edge with real banner HTML. That creates a mismatch by design. The browser sees the edge-modified HTML, but React hydrates using the original React output. So the short answer is:
Hope this helps. |
|
Hi @kuldeeprajput-dev , Thanks for the quick response. My ultimate goal is to render everything through SSR, with no client-side rendering, to maximize SEO benefits. The banner is created and managed in a separate application/language, and I want any inserted component or reusable widget to be automatically reflected across all consuming applications. For example, if I embed a banner/widget from the edge layer into another application, any updates made to that banner should automatically propagate and update the rendered DOM everywhere it has been integrated, without requiring manual changes in the consuming applications. Could you please let me know the best approach to achieve this architecture? |
|
I do believe you could also do something like: export default function Home() {
return (
<div>
<h1>Hello</h1>
<div
suppressHydrationWarning
data-akamai-include-url="/banner-url"
dangerouslySetInnerHTML={{
__html: " ",
}}
/>
</div>
);
}Then intercept/find this node on your Edge Worker, and use the data attributes to inject the content? Or actually, is it like, if you keep the |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Hi,
I'm using Next.js App Router with SSR and trying to inject a dynamic banner into the HTML response using an Akamai EdgeWorker.
The SSR page contains a placeholder element like:
In the EdgeWorker, I intercept the HTML response, fetch content from the URL specified in the
akamai:includetag, and replace the tag with the fetched HTML before the response is sent to the browser.The banner content is rendered correctly in the initial HTML response and is visible when the page first loads. However, once React hydrates the page, the injected content is removed and replaced with the original markup that was generated by Next.js, resulting in a hydration mismatch.
My understanding is that React is reconciling against the server-rendered React tree and doesn't know about the HTML modifications made by the EdgeWorker after SSR.
Has anyone successfully implemented edge-side HTML injection with Next.js App Router without triggering hydration issues?
Some questions I have:
suppressHydrationWarninghelp in this scenario, or is it only intended for text/content differences?Example flow:
EdgeWorker transforms response to:
Browser initially displays the banner.
React hydrates and restores the original React tree, removing the injected content.
Any guidance or recommended architecture would be appreciated.
Additional information
No response
Example
No response
All reactions