Any event targeting a non-React DOM node inside an already-hydrated <!--$-->...<!--/$--> or <!--&-->...<!--/&--> range — e.g. Chrome/Google Translate wrapping hydrated text in <font><font>...</font></font> inside , -inserted nodes, extensions/imperative insertions, or foreign nodes between leftover comments after conditionally unmounting inside a still-mounted parent with a surviving child — is for the host parent and all ancestors (and continuous events like before a re-render also call ).
React version:19.3.1 (react, react-dom, dev & prod) and main (edae846a9a17; v19.3.1 logic is identical at ReactFiberConfigDOM.js +8 / clearHydrationBoundary L1240 and ReactFiberTreeReflection.js +1).
Root Cause
Stale pointer left on comment markers: Pass 1 caches fiber_pass1 on commentNode (internalInstanceKey / internalInstanceMap); pass 2 commits the hydrated tree (current.memoizedState = null) without detaching commentNode.
Broken assumption in getClosestInstanceFromNode: For a non-React target, React walks up to the host parent (targetInst.child !== null) and calls getParentHydrationBoundary. L192–L197 assumes hydrated comments are unreachable, returning the SuspenseComponent/ActivityComponent fiber instead of the host parent.
Before a subsequent re-render:fiber_pass1 is current.alternate (still holding memoizedState.dehydrated === commentNode), so getSuspenseInstanceFromFiber / getActivityInstanceFromFiber returns commentNode. Discrete events enter dispatchEvent, run wasted attemptSynchronousHydration, re-run findInstanceBlockingTarget, hit instance === null -> targetInst = null (L276/L289), and drop the event. Continuous events (mouseover, pointerover, focusin) enter L180–L191, queue on the already-hydrated comment, and call nativeEvent.stopPropagation().
After any re-render (or first dropped event): Both alternates have memoizedState === null, so findInstanceBlockingTarget sets targetInst = null (L276/L289) and drops all React events forever.
After conditional unmount inside a mounted parent: Unlike root.unmount() (which empties the container), show ? <Suspense>...</Suspense> : null beside a surviving child leaves <!--$--> retaining the unmounted fiber (getNearestMountedFiber === null), leaking the fiber and dropping events on foreign nodes between those comments.
Reproduced identically in Playwright Chromium (dev & prod [email protected]), including <span>-wrapped <font>, nested hydrated outer + dehydrated inner <Suspense>, and conditional unmount inside a mounted parent with a surviving child.
Expected Behavior
After hydration, events on non-React child nodes resolve to the nearest React host ancestor (clicks=[1,2,3], mouseEnters=1, bodyMouseOver=1, contentEditableclicks=2 inputs=2), while still-dehydrated boundaries keep blocking parent dispatch and triggering selective hydration.
Proposed Fix
Add detachHydratedInstance(node: SuspenseInstance | ActivityInstance) in ReactDOMComponentTree.js (handling both enableInternalInstanceMap = true and false) and call it beforeretryIfBlockedOn(...) in commitHydratedActivityInstance, commitHydratedSuspenseInstance, and clearHydrationBoundary. retryIfBlockedOn matches queued entries by comment node identity (===), while still-dehydrated boundaries keep their cached fiber (cf. closed PR #16591, which intentionally blocked events while dehydrated).
diff --git a/packages/react-dom-bindings/src/client/ReactDOMComponentTree.js b/packages/react-dom-bindings/src/client/ReactDOMComponentTree.js
--- a/packages/react-dom-bindings/src/client/ReactDOMComponentTree.js
+++ b/packages/react-dom-bindings/src/client/ReactDOMComponentTree.js
@@ -91,6 +91,19 @@ export function detachDeletedInstance(node: Instance): void {
delete (node as any)[internalEventHandlesSetKey];
}
+export function detachHydratedInstance(
+ node: SuspenseInstance | ActivityInstance,
+): void {
+ if (enableInternalInstanceMap) {
+ internalInstanceMap.delete(node);
+ if (__DEV__) {
+ delete (node as any)[internalInstanceKey];
+ }
+ return;
+ }
+ delete (node as any)[internalInstanceKey];
+}
+
export function precacheFiberNode(
hostInst: Fiber,
node:
@@ -189,14 +202,11 @@ export function getClosestInstanceFromNode(targetNode: Node): null | Fiber {
// nested within a dehydrated boundary and if so, which one.
let hydrationInstance = getParentHydrationBoundary(targetNode);
while (hydrationInstance !== null) {
- // We found a suspense instance. That means that we haven't
- // hydrated it yet. Even though we leave the comments in the
- // DOM after hydrating, and there are boundaries in the DOM
- // that could already be hydrated, we wouldn't have found them
- // through this pass since if the target is hydrated it would
- // have had an internalInstanceKey on it.
- // Let's get the fiber associated with the SuspenseComponent
- // as the deepest instance.
+ // We found a hydration boundary comment. Once a boundary finishes
+ // hydrating, its fiber pointer is detached from the comment, so
+ // non-React DOM nodes inserted inside an already-hydrated boundary
+ // skip past it. If a fiber is still cached, the boundary is still
+ // dehydrated.
const targetFiber = enableInternalInstanceMap
? internalInstanceMap.get(hydrationInstance)
: // $FlowFixMe[prop-missing]
diff --git a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
--- a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
+++ b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
@@ -53,6 +53,7 @@ import {
getInstanceFromNode as getInstanceFromNodeDOMTree,
isContainerMarkedAsRoot,
detachDeletedInstance,
+ detachHydratedInstance,
getResourcesFromRoot,
isMarkedHoistable,
markNodeAsHoistable,
@@ -1236,6 +1237,7 @@ function clearHydrationBoundary(
parentInstance: Instance,
hydrationInstance: SuspenseInstance | ActivityInstance,
): void {
+ detachHydratedInstance(hydrationInstance);
let node: Node = hydrationInstance;
// Delete all nodes within this suspense boundary.
// There might be nested nodes so we need to keep track of how
@@ -4739,6 +4741,7 @@ export function commitHydratedContainer(container: Container): void {
export function commitHydratedActivityInstance(
activityInstance: ActivityInstance,
): void {
+ detachHydratedInstance(activityInstance);
// Retry if any event replaying was blocked on this.
retryIfBlockedOn(activityInstance);
}
@@ -4746,6 +4749,7 @@ export function commitHydratedActivityInstance(
export function commitHydratedSuspenseInstance(
suspenseInstance: SuspenseInstance,
): void {
+ detachHydratedInstance(suspenseInstance);
// Retry if any event replaying was blocked on this.
retryIfBlockedOn(suspenseInstance);
}
Verification
git apply --check --whitespace=error applies cleanly on main (edae846a9a17).
Patched dev & prod [email protected] output of repro.js (also verified in Playwright Chromium, incl. nested hydrated outer + dehydrated inner <Suspense> and conditional unmount inside a mounted parent):