Skip to content

Commit c60e71a

Browse files
committed
fix(hydration): fix keepsText deferral for null nodes, settle tracking on first match, and hempty frame unwrapping
Bug 1 — keepsText now defers null-node mismatches when rendersAgain() is true instead of throwing HydrationMismatch immediately. A new deferredNullText field stashes the pending server/loc, and callers register the newly created Text node via trackDeferredText so settleValues can compare the settled text. Bug 2 — keepsText registers the Text node in pendingTexts even when the first render matches the server, so a later render-phase update that diverges is caught by settleValues instead of silently patching. Bug 3 — hempty now unwraps a text-only frame (open + text + close) the same way htext does, then re-enters, so a framed server primitive against an empty client value goes through keepsText instead of falling to unclaimedText.
1 parent 7d5843a commit c60e71a

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19222,6 +19222,8 @@ class HydrationCapability {
1922219222
private classes: Map<Element, PendingHydrationClass> | null = null;
1922319223
/** Server texts compared once the pass's render-phase updates settle (keepsText). */
1922419224
private pendingTexts: Map<Text, PendingHydrationText> | null = null;
19225+
/** Stashed when keepsText defers a null-node mismatch; claimed by trackDeferredText. */
19226+
private deferredNullText: PendingHydrationText | null = null;
1922519227
/**
1922619228
* DEV-only: the server values this pass kept where the client's differ, by
1922719229
* their warning line, with the node each belongs to (settleValues).
@@ -19932,6 +19934,7 @@ class HydrationCapability {
1993219934
const keep = text !== '' && this.keepsText(host, null, '', text, (host as any)?.__oct_loc);
1993319935
const node = (STAGED_DOM?.view(document) ?? document).createTextNode(keep ? '' : text);
1993419936
domNode((STAGED_DOM?.view(close) ?? close).parentNode)!.insertBefore(node, close);
19937+
this.trackDeferredText(node);
1993519938
return node;
1993619939
}
1993719940

@@ -20314,24 +20317,32 @@ class HydrationCapability {
2031420317
text: string,
2031520318
loc: string | undefined,
2031620319
): boolean {
20317-
if (server === text || isTextParserNormalizedMatch(server, text)) return true;
20320+
const match = server === text || isTextParserNormalizedMatch(server, text);
2031820321
if (
20322+
!match &&
2031920323
host !== null &&
2032020324
(this.freshNodes.has(host) ||
2032120325
(host.nodeType === 1 && (host as Element).localName === 'textarea'))
2032220326
)
2032320327
return false;
20324-
if (!this.staleServerValues && isHydrationSuppressed(host)) return true;
20325-
if (node !== null && node.nodeType === 3 && this.rendersAgain()) {
20326-
const pending = (this.pendingTexts ??= new Map());
20327-
if (!pending.has(node as Text)) pending.set(node as Text, { server, loc });
20328-
if (inRootHydrationAttempt()) journalText(node as Text, server);
20329-
if (this.speculative)
20330-
(this.undo ??= []).push(() => {
20331-
(STAGED_DOM?.view(node) ?? node).nodeValue = server;
20332-
});
20333-
return false;
20328+
if (!match && !this.staleServerValues && isHydrationSuppressed(host)) return true;
20329+
if (this.rendersAgain()) {
20330+
if (node !== null && node.nodeType === 3) {
20331+
const pending = (this.pendingTexts ??= new Map());
20332+
if (!pending.has(node as Text)) pending.set(node as Text, { server, loc });
20333+
if (!match) {
20334+
if (inRootHydrationAttempt()) journalText(node as Text, server);
20335+
if (this.speculative)
20336+
(this.undo ??= []).push(() => {
20337+
(STAGED_DOM?.view(node) ?? node).nodeValue = server;
20338+
});
20339+
}
20340+
} else if (node === null && !match) {
20341+
this.deferredNullText = { server, loc };
20342+
}
20343+
return match;
2033420344
}
20345+
if (match) return true;
2033520346
if (PRESENTATION_HYDRATION?.revision !== undefined) presentationMiss();
2033620347
this.mismatch(
2033720348
loc || (host as any)?.__oct_loc,
@@ -20340,6 +20351,19 @@ class HydrationCapability {
2034020351
);
2034120352
}
2034220353

20354+
/**
20355+
* Claims the text node that a caller created after keepsText deferred a
20356+
* null-node mismatch: registers the node in pendingTexts so settleValues
20357+
* compares the settled text against the server's.
20358+
*/
20359+
trackDeferredText(node: Text): void {
20360+
const deferred = this.deferredNullText;
20361+
if (deferred !== null) {
20362+
this.deferredNullText = null;
20363+
(this.pendingTexts ??= new Map()).set(node, deferred);
20364+
}
20365+
}
20366+
2034320367
/**
2034420368
* Whether the block rendering now, or an ancestor of it in this pass, has an
2034520369
* update queued, which renders it again before the pass ends
@@ -20879,6 +20903,7 @@ class HydrationCapability {
2087920903
const keep = text !== '' && this.keepsText(el, null, '', text, loc);
2088020904
const created = (STAGED_DOM?.view(document) ?? document).createTextNode(keep ? '' : text);
2088120905
(STAGED_DOM?.view(el) ?? el).appendChild(created);
20906+
this.trackDeferredText(created);
2088220907
return created;
2088320908
}
2088420909

@@ -20957,12 +20982,16 @@ class HydrationCapability {
2095720982
const first = getFirstChild(el);
2095820983
if (first === null || (el as Element).localName === 'textarea' || this.freshNodes.has(el))
2095920984
return null;
20960-
const next = getNextSibling(first);
20961-
if (this.isOpen(first) && this.isClose(next) && getNextSibling(next) === null) {
20962-
(STAGED_DOM?.view(first) ?? first).remove();
20963-
(STAGED_DOM?.view(next) ?? next).remove();
20964-
return null;
20985+
if (this.isOpen(first)) {
20986+
const child = getNextSibling(first);
20987+
const end = child?.nodeType === 3 ? getNextSibling(child) : child;
20988+
if (this.isClose(end) && getNextSibling(end) === null) {
20989+
(STAGED_DOM?.view(first) ?? first).remove();
20990+
(STAGED_DOM?.view(end) ?? end).remove();
20991+
return this.hempty(el, loc);
20992+
}
2096520993
}
20994+
const next = getNextSibling(first);
2096620995
if (first.nodeType === 3 && next === null) {
2096720996
if (!this.keepsText(el, first, (STAGED_DOM?.view(first) ?? first).nodeValue, '', loc))
2096820997
(STAGED_DOM?.view(first) ?? first).nodeValue = '';
@@ -20998,6 +21027,7 @@ class HydrationCapability {
2099821027
if (posNode !== null && (STAGED_DOM?.view(posNode) ?? posNode).parentNode !== null) {
2099921028
domNode((STAGED_DOM?.view(posNode) ?? posNode).parentNode!).insertBefore(created, posNode);
2100021029
}
21030+
this.trackDeferredText(created);
2100121031
return created;
2100221032
}
2100321033

@@ -37318,6 +37348,7 @@ export function childSlot(
3731837348
const tn = (STAGED_DOM?.view(document) ?? document).createTextNode(str);
3731937349
(STAGED_DOM?.view(domParent) ?? domParent).insertBefore(tn, state.end);
3732037350
state.text = tn;
37351+
if (hydration !== null) hydration.trackDeferredText(tn);
3732137352
}
3732237353

3732337354
// A `{expr}` value-hole slot whose value is USUALLY a primitive (text) — what the

0 commit comments

Comments
 (0)