From ed5f010ae51db1544ce92e1a5105e870b5a5098e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 16 Oct 2019 16:23:12 -0700 Subject: [PATCH] Client render Suspense content if there's no boundary match (#16945) Without the enableSuspenseServerRenderer flag there will never be a boundary match. Also when it is enabled, there might not be a boundary match if something was conditionally rendered by mistake. With this PR it will now client render the content of a Suspense boundary in that case and issue a DEV only hydration warning. This is the only sound semantics for this case. Unfortunately, landing this will once again break #16938. It will be less bad though because at least it'll just work by client rendering the content instead of hydrating and issue a DEV only warning. However, we must land this before enabling the enableSuspenseServerRenderer flag since it does this anyway. I did notice that we special case fallback={undefined} due to our unfortunate semantics for that. So technically a workaround that works is actually setting the fallback to undefined on the server and during hydration. Then flip it on only after hydration. That could be a workaround if you want to be able to have a Suspense boundary work only after hydration for some reason. It's kind of unfortunate but at least those semantics are internally consistent. So I added a test for that. --- .../ReactServerRenderingHydration-test.js | 35 +++++++++++++++++-- .../src/ReactFiberBeginWork.js | 12 +++---- .../src/ReactFiberCompleteWork.js | 7 ++-- .../src/ReactFiberHydrationContext.js | 9 ++--- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js b/packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js index b39feffb90..834f9af4df 100644 --- a/packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js +++ b/packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js @@ -517,14 +517,45 @@ describe('ReactDOMServerHydration', () => { }); } - it('regression test: Suspense + hydration in legacy mode ', () => { + it('Suspense + hydration in legacy mode', () => { const element = document.createElement('div'); element.innerHTML = '
Hello World
'; + let div = element.firstChild; + let ref = React.createRef(); + expect(() => + ReactDOM.hydrate( + +
Hello World
+
, + element, + ), + ).toWarnDev( + 'Warning: Did not expect server HTML to contain a
in
.', + {withoutStack: true}, + ); + + // The content should've been client rendered and replaced the + // existing div. + expect(ref.current).not.toBe(div); + // The HTML should be the same though. + expect(element.innerHTML).toBe('
Hello World
'); + }); + + it('Suspense + hydration in legacy mode with no fallback', () => { + const element = document.createElement('div'); + element.innerHTML = '
Hello World
'; + let div = element.firstChild; + let ref = React.createRef(); ReactDOM.hydrate( -
Hello World
+
Hello World
, element, ); + + // Because this didn't have a fallback, it was hydrated as if it's + // not a Suspense boundary. + expect(ref.current).toBe(div); + expect(element.innerHTML).toBe('
Hello World
'); }); }); diff --git a/packages/react-reconciler/src/ReactFiberBeginWork.js b/packages/react-reconciler/src/ReactFiberBeginWork.js index a6855d9b90..a6ee56a090 100644 --- a/packages/react-reconciler/src/ReactFiberBeginWork.js +++ b/packages/react-reconciler/src/ReactFiberBeginWork.js @@ -1592,12 +1592,12 @@ function updateSuspenseComponent( // children. It's essentially a very basic form of re-parenting. if (current === null) { - if (enableSuspenseServerRenderer) { - // If we're currently hydrating, try to hydrate this boundary. - // But only if this has a fallback. - if (nextProps.fallback !== undefined) { - tryToClaimNextHydratableInstance(workInProgress); - // This could've been a dehydrated suspense component. + // If we're currently hydrating, try to hydrate this boundary. + // But only if this has a fallback. + if (nextProps.fallback !== undefined) { + tryToClaimNextHydratableInstance(workInProgress); + // This could've been a dehydrated suspense component. + if (enableSuspenseServerRenderer) { const suspenseState: null | SuspenseState = workInProgress.memoizedState; if (suspenseState !== null) { diff --git a/packages/react-reconciler/src/ReactFiberCompleteWork.js b/packages/react-reconciler/src/ReactFiberCompleteWork.js index 3c3db07d80..2436ec6af6 100644 --- a/packages/react-reconciler/src/ReactFiberCompleteWork.js +++ b/packages/react-reconciler/src/ReactFiberCompleteWork.js @@ -870,10 +870,9 @@ function completeWork( const nextDidTimeout = nextState !== null; let prevDidTimeout = false; if (current === null) { - // In cases where we didn't find a suitable hydration boundary we never - // put this in dehydrated mode, but we still need to pop the hydration - // state since we might be inside the insertion tree. - popHydrationState(workInProgress); + if (workInProgress.memoizedProps.fallback !== undefined) { + popHydrationState(workInProgress); + } } else { const prevState: null | SuspenseState = current.memoizedState; prevDidTimeout = prevState !== null; diff --git a/packages/react-reconciler/src/ReactFiberHydrationContext.js b/packages/react-reconciler/src/ReactFiberHydrationContext.js index 61f007daca..018a80886e 100644 --- a/packages/react-reconciler/src/ReactFiberHydrationContext.js +++ b/packages/react-reconciler/src/ReactFiberHydrationContext.js @@ -404,10 +404,11 @@ function skipPastDehydratedSuspenseInstance( let suspenseState: null | SuspenseState = fiber.memoizedState; let suspenseInstance: null | SuspenseInstance = suspenseState !== null ? suspenseState.dehydrated : null; - if (suspenseInstance === null) { - // This Suspense boundary was hydrated without a match. - return nextHydratableInstance; - } + invariant( + suspenseInstance, + 'Expected to have a hydrated suspense instance. ' + + 'This error is likely caused by a bug in React. Please file an issue.', + ); return getNextHydratableInstanceAfterSuspenseInstance(suspenseInstance); }