Skip to content

Commit 591f3a4

Browse files
committed
fix(hydration): resume a client-built suspended leaf without adopting server nodes
When claimRoots rejects a fragment and rebuildFragment builds it on the client, a child that suspends via use() inside that rebuilt subtree is client-built. The preserved hydrate activation must record that fact and resume the source inside suspend() so activeHydration() returns null, preventing the leaf from adopting at the saved cursor. This fix was originally part of 3a6a06d but was lost when PR #1599 merged from origin/main, which modified the same claimRoots area without the freshSuspension tracking. Re-applies the three-part mechanism: suspend() records SuspenseExceptions that pass through it on freshSuspension, preserveSuspendedHydrateActivation compares the exception identity to set the activation's fresh flag, and the resume path wraps renderBlock in suspend() when fresh is true.
1 parent f757a7d commit 591f3a4

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14778,6 +14778,8 @@ interface PreservedHydrateActivation {
1477814778
capture: OffscreenCapture;
1477914779
source: Block | null;
1478014780
cursor: Node | null;
14781+
/** The source mounted inside client-built DOM, which it resumes as. */
14782+
fresh: boolean;
1478114783
}
1478214784

1478314785
// Allocate bookkeeping only when an already-visible SSR arm really suspends.
@@ -14929,8 +14931,9 @@ function findSuspendedHydrateBlock(scope: Scope, thenable: TrackedThenable<unkno
1492914931
function preserveSuspendedHydrateActivation(
1493014932
state: HydrateSlot,
1493114933
hydration: HydrationCapability,
14932-
thenable: TrackedThenable<unknown>,
14934+
suspension: SuspenseException,
1493314935
): void {
14936+
const thenable = suspension.thenable;
1493414937
const suspendedBlock = findSuspendedHydrateBlock(state.block, thenable);
1493514938
const activation: PreservedHydrateActivation = {
1493614939
hydration,
@@ -14939,7 +14942,9 @@ function preserveSuspendedHydrateActivation(
1493914942
capture: WIP_CAPTURE!,
1494014943
source: suspendedBlock === state.block ? null : suspendedBlock,
1494114944
cursor: hydration.resumeAt(),
14945+
fresh: hydration.freshSuspension === suspension,
1494214946
};
14947+
hydration.freshSuspension = null;
1494314948
const activations = (preservedHydrateActivations ??= new WeakMap());
1494414949
if (!activations.has(state)) preservedHydrateActivationCount++;
1494514950
activations.set(state, activation);
@@ -15129,7 +15134,7 @@ function createHydrateBoundaryBody(
1512915134
// The SSR arm is already the visible content. Let its adopted try block
1513015135
// remain connected and retry adoption in the same hydration capability
1513115136
// once application data settles, without ever mounting a cloned fallback.
15132-
preserveSuspendedHydrateActivation(state, hydration, error.thenable);
15137+
preserveSuspendedHydrateActivation(state, hydration, error);
1513315138
return;
1513415139
}
1513515140
state.hydrated = true;
@@ -15893,12 +15898,16 @@ function activateHydrateBoundary(state: HydrateSlot): void {
1589315898
// ranges on an ordinary parent replay. Complete the actually suspended
1589415899
// leaf at its saved cursor first, then let its ancestors reconcile the
1589515900
// already-adopted siblings without treating their range as fresh DOM.
15901+
// A leaf that mounted inside a mismatch's client-built replacement has
15902+
// no server DOM to adopt and completes as a client render.
15903+
const source = preserved.source;
1589615904
hydration.node = preserved.cursor;
1589715905
try {
15898-
renderBlock(preserved.source);
15906+
if (preserved.fresh) hydration.suspend(() => renderBlock(source));
15907+
else renderBlock(source);
1589915908
} catch (error) {
1590015909
if (!isSuspenseException(error)) throw error;
15901-
preserveSuspendedHydrateActivation(state, hydration, error.thenable);
15910+
preserveSuspendedHydrateActivation(state, hydration, error);
1590215911
return;
1590315912
}
1590415913
}
@@ -18858,6 +18867,8 @@ class HydrationCapability {
1885818867
* does not claim. Undefined until the template adopts claimFrom.
1885918868
*/
1886018869
private claimEnd: Node | null | undefined = undefined;
18870+
/** The last suspension thrown out of a client-built subtree (suspend). */
18871+
freshSuspension: SuspenseException | null = null;
1886118872
/** Skip component-frame adoption until the declared container owner. */
1886218873
passthroughRanges = false;
1886318874
/**
@@ -18992,6 +19003,9 @@ class HydrationCapability {
1899219003
const previousNative = setNativeAdoptionResolver(null);
1899319004
try {
1899419005
return fn();
19006+
} catch (error) {
19007+
if (isSuspenseException(error)) this.freshSuspension = error;
19008+
throw error;
1899519009
} finally {
1899619010
setNativeAdoptionResolver(previousNative);
1899719011
this.depth--;

0 commit comments

Comments
 (0)