AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Streaming Metadata Wrapper Uses <Div> Causing Hydration Deletion When Third-Party Prepends Same Tag

Next.js Streaming Metadata renders a <div hidden> as the first child of <body>. React 19 hydration matches only by tag name and claims any prepended <div> (e.g. consent manager or chat widget) as the metadata wrapper. Hydration then fails with Minified React error #418, and React's clearContainerSparingly removes the third-party DOM node.

highConfidence 95%Next.jsAffected V16.3.4Affected V16.4.0-Canary.25

Origin Analysis

Next.js's MetadataWrapper in packages/next/src/lib/metadata/metadata.tsx uses a plain <div> as the host element for streaming metadata. React 19's canHydrateInstance skips foreign nodes only when the tag name differs. Because <div> is the most common tag prepended by third-party scripts before hydration, the wrapper collides with those nodes, causing hydration mismatch and deletion.
1. Create an App Router project with streaming metadata (default).\n2. In app/page.tsx add an inline script that prepends <div id="third-party"> to document.body during HTML parsing.\n3. Build and start the app, open the page with console open.\n4. Observe Minified React error #418 and that #third-party is removed from the DOM.\n5. Change the prepended tag from 'div' to 'aside' and repeat; no error occurs and the node is kept.

Fixing Code Block

Edge Case Audit

This change alters the server-rendered HTML from <div hidden> to <next-metadata hidden>. Ensure the custom element is recognized by TypeScript JSX (Next.js already supports similar tags like <next-route-announcer>). Test in all supported browsers; while custom elements are widely supported, legacy browsers or strict HTML parsers may treat it differently. If the custom element introduces any styling or hydration edge case, rollback by reverting to <div> and consider a different mitigation such as rendering metadata outside <body> or using a more unique standard element. No concurrency or multi-thread issue is introduced; the race condition remains but is made harmless.

Ecosystem Topology