Skip to content

[Hydration] Bail out from hydrating suspense boundary if component suspends #22567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ describe('ReactDOMServerPartialHydration', () => {
);

// This will throw it away and rerender.
expect(Scheduler).toFlushAndYield(['Child', 'Sibling']);
expect(Scheduler).toFlushAndYield(['Child']);

expect(container.textContent).toBe('Hello');

Expand Down Expand Up @@ -2738,4 +2738,57 @@ describe('ReactDOMServerPartialHydration', () => {
expect(ref.current).toBe(span);
expect(ref.current.innerHTML).toBe('Hidden child');
});

it('can hydrate siblings of a suspended component without errors', async () => {
let suspend = false;
let resolve;
const promise = new Promise(resolvePromise => (resolve = resolvePromise));
function Child() {
if (suspend) {
throw promise;
} else {
return 'Hello';
}
}

function App() {
return (
<Suspense fallback="Loading...">
<Child />
<Suspense fallback="Loading...">
<div>Hello</div>
</Suspense>
</Suspense>
);
}

// First we render the final HTML. With the streaming renderer
// this may have suspense points on the server but here we want
// to test the completed HTML. Don't suspend on the server.
suspend = false;
const finalHTML = ReactDOMServer.renderToString(<App />);

const container = document.createElement('div');
container.innerHTML = finalHTML;
expect(container.textContent).toBe('HelloHello');

// On the client we don't have all data yet but we want to start
// hydrating anyway.
suspend = true;
ReactDOM.hydrateRoot(container, <App />);
Scheduler.unstable_flushAll();
jest.runAllTimers();

// Expect the server-generated HTML to stay intact.
expect(container.textContent).toBe('HelloHello');

// Resolving the promise should continue hydration
suspend = false;
resolve();
await promise;
Scheduler.unstable_flushAll();
jest.runAllTimers();
// Hydration should not change anything.
expect(container.textContent).toBe('HelloHello');
});
});
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent.new';
import type {StackCursor} from './ReactFiberStack.new';
import type {Flags} from './ReactFiberFlags';
import type {FunctionComponentUpdateQueue} from './ReactFiberHooks.new';
import {getIsHydrating} from './ReactFiberHydrationContext.new';

import {
warnAboutDeprecatedLifecycles,
Expand Down Expand Up @@ -1746,7 +1747,11 @@ function completeUnitOfWork(unitOfWork: Fiber): void {
}

const siblingFiber = completedWork.sibling;
if (siblingFiber !== null) {
const isHydrating = getIsHydrating();
if (
siblingFiber !== null &&
(!isHydrating || (completedWork.flags & Incomplete) === NoFlags)
) {
// If there is more work to do in this returnFiber, do that next.
workInProgress = siblingFiber;
return;
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {SuspenseState} from './ReactFiberSuspenseComponent.old';
import type {StackCursor} from './ReactFiberStack.old';
import type {Flags} from './ReactFiberFlags';
import type {FunctionComponentUpdateQueue} from './ReactFiberHooks.old';
import {getIsHydrating} from './ReactFiberHydrationContext.old';

import {
warnAboutDeprecatedLifecycles,
Expand Down Expand Up @@ -1746,7 +1747,11 @@ function completeUnitOfWork(unitOfWork: Fiber): void {
}

const siblingFiber = completedWork.sibling;
if (siblingFiber !== null) {
const isHydrating = getIsHydrating();
if (
siblingFiber !== null &&
(!isHydrating || (completedWork.flags & Incomplete) === NoFlags)
) {
// If there is more work to do in this returnFiber, do that next.
workInProgress = siblingFiber;
return;
Expand Down