Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ import {
pushFallbackTreeSuspenseHandler,
pushOffscreenSuspenseHandler,
reuseSuspenseHandlerOnStack,
popSuspenseHandler,
} from './ReactFiberSuspenseContext';
import {
pushHiddenContext,
Expand Down Expand Up @@ -245,7 +244,7 @@ import {
claimHydratableSingleton,
tryToClaimNextHydratableInstance,
tryToClaimNextHydratableTextInstance,
tryToClaimNextHydratableSuspenseInstance,
claimNextHydratableSuspenseInstance,
warnIfHydrating,
queueHydrationError,
} from './ReactFiberHydrationContext';
Expand Down Expand Up @@ -2151,24 +2150,14 @@ function updateSuspenseComponent(
} else {
pushFallbackTreeSuspenseHandler(workInProgress);
}
tryToClaimNextHydratableSuspenseInstance(workInProgress);
// This could've been a dehydrated suspense component.
const suspenseState: null | SuspenseState = workInProgress.memoizedState;
if (suspenseState !== null) {
const dehydrated = suspenseState.dehydrated;
if (dehydrated !== null) {
return mountDehydratedSuspenseComponent(
workInProgress,
dehydrated,
renderLanes,
);
}
}
// If hydration didn't succeed, fall through to the normal Suspense path.
// To avoid a stack mismatch we need to pop the Suspense handler that we
// pushed above. This will become less awkward when move the hydration
// logic to its own fiber.
popSuspenseHandler(workInProgress);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path became unreachable once we started throwing for all hydration mismatches but we didn't know that due to the code structure.

// This throws if we fail to hydrate.
const dehydrated: SuspenseInstance =
claimNextHydratableSuspenseInstance(workInProgress);
return mountDehydratedSuspenseComponent(
workInProgress,
dehydrated,
renderLanes,
);
}

const nextPrimaryChildren = nextProps.children;
Expand Down
23 changes: 13 additions & 10 deletions packages/react-reconciler/src/ReactFiberHydrationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ function tryHydrateText(fiber: Fiber, nextInstance: any) {
return false;
}

function tryHydrateSuspense(fiber: Fiber, nextInstance: any) {
function tryHydrateSuspense(
fiber: Fiber,
nextInstance: any,
): null | SuspenseInstance {
// fiber is a SuspenseComponent Fiber
const suspenseInstance = canHydrateSuspenseInstance(
nextInstance,
Expand All @@ -298,9 +301,8 @@ function tryHydrateSuspense(fiber: Fiber, nextInstance: any) {
// While a Suspense Instance does have children, we won't step into
// it during the first pass. Instead, we'll reenter it later.
nextHydratableInstance = null;
return true;
}
return false;
return suspenseInstance;
}

export const HydrationMismatchException: mixed = new Error(
Expand Down Expand Up @@ -423,15 +425,16 @@ function tryToClaimNextHydratableTextInstance(fiber: Fiber): void {
}
}

function tryToClaimNextHydratableSuspenseInstance(fiber: Fiber): void {
if (!isHydrating) {
return;
}
function claimNextHydratableSuspenseInstance(fiber: Fiber): SuspenseInstance {
const nextInstance = nextHydratableInstance;
if (!nextInstance || !tryHydrateSuspense(fiber, nextInstance)) {
const suspenseInstance = nextInstance
? tryHydrateSuspense(fiber, nextInstance)
: null;
if (suspenseInstance === null) {
warnNonHydratedInstance(fiber, nextInstance);
throwOnHydrationMismatch(fiber);
throw throwOnHydrationMismatch(fiber);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Satisfies Flow. Does Flow not have something like never where this is asserted to always throw?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work

}
return suspenseInstance;
}

export function tryToClaimNextHydratableFormMarkerInstance(
Expand Down Expand Up @@ -790,7 +793,7 @@ export {
claimHydratableSingleton,
tryToClaimNextHydratableInstance,
tryToClaimNextHydratableTextInstance,
tryToClaimNextHydratableSuspenseInstance,
claimNextHydratableSuspenseInstance,
prepareToHydrateHostInstance,
prepareToHydrateHostTextInstance,
prepareToHydrateHostSuspenseInstance,
Expand Down
Loading