From 9ae80d6a2bf8f48f20e3d62b9672f21c1ff77bd8 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Wed, 20 Apr 2022 14:52:54 -0700 Subject: [PATCH] Suppress hydration warnings when a preceding sibling suspends (#24404) * Add failing test case for #24384 If a components suspends during hydration we expect there to be mismatches with server rendered HTML but we were not always supressing warning messages related to these expected mismatches * Mark hydration as suspending on every thrownException previously hydration would only be marked as supsending when a genuine error was thrown. This created an opportunity for a hydration mismatch that would warn after which later hydration mismatches would not lead to warnings. By moving the marker check earlier in the thrownException function we get the hydration context to enter the didSuspend state on both error and thrown promise cases which eliminates this gap. * Fix failing test related to client render fallbacks This test was actually subject to the project identified in the issue fixed in this branch. After fixing the underlying issue the assertion logic needed to change to pick the right warning which now emits after hydration successfully completes on promise resolution. I changed the container type to 'section' to make the error message slightly easier to read/understand (for me) * Only mark didSuspend on suspense path For unknown reasons the didSuspend was being set only on the error path and nto the suspense path. The original change hoisted this to happen on both paths. This change moves the didSuspend call to the suspense path only. This appears to be a noop because if the error path marked didSuspend it would suppress later warnings but if it does not the warning functions themsevles do that suppression (after the first one which necessarily already happened) * gate test on hydration fallback flags * refactor didSuspend to didSuspendOrError the orignial behavior applied the hydration warning bailout to error paths only. originally I moved it to Suspense paths only but this commit restores it to both paths and renames the marker function as didThrow rather than didSuspend The logic here is that for either case if we get a mismatch in hydration we want to warm up components but effectively consider the hydration for this boundary halted * factor tests to assert different behavior between prod and dev * add DEV suffix to didSuspendOrError to better indicate this feature should only affect dev behavior * move tests back to ReactDOMFizzServer-test * fix comment casing * drop extra flag gates in tests * add test for user error case * remove unnecessary gate * Make test better it now has an intentional client mismatch that would error if there wasn't suppression brought about by the earlier error. when it client renders it has the updated value not found in the server response but we do not see a hydration warning because it was superseded by the thrown error in that render --- .../src/__tests__/ReactDOMFizzServer-test.js | 243 ++++++++++++++++++ ...DOMServerPartialHydration-test.internal.js | 12 +- .../src/ReactFiberHydrationContext.new.js | 21 +- .../src/ReactFiberHydrationContext.old.js | 21 +- .../src/ReactFiberThrow.new.js | 11 +- .../src/ReactFiberThrow.old.js | 11 +- 6 files changed, 290 insertions(+), 29 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js index bc609efcf0d0..bf5388597571 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js @@ -2841,4 +2841,247 @@ describe('ReactDOMFizzServer', () => { expect(window.__test_outlet).toBe(1); }); }); + + // @gate experimental && enableClientRenderFallbackOnTextMismatch + it('#24384: Suspending should halt hydration warnings while still allowing siblings to warm up', async () => { + const makeApp = () => { + let resolve, resolved; + const promise = new Promise(r => { + resolve = () => { + resolved = true; + return r(); + }; + }); + function ComponentThatSuspends() { + if (!resolved) { + throw promise; + } + return

A

; + } + + const App = ({text}) => { + return ( +
+ Loading...}> + +

{text}

+
+
+ ); + }; + + return [App, resolve]; + }; + + const [ServerApp, serverResolve] = makeApp(); + await act(async () => { + const {pipe} = ReactDOMFizzServer.renderToPipeableStream( + , + ); + pipe(writable); + }); + await act(() => { + serverResolve(); + }); + + expect(getVisibleChildren(container)).toEqual( +
+

A

+

initial

+
, + ); + + // The client app is rendered with an intentionally incorrect text. The still Suspended component causes + // hydration to fail silently (allowing for cache warming but otherwise skipping this boundary) until it + // resolves. + const [ClientApp, clientResolve] = makeApp(); + ReactDOMClient.hydrateRoot(container, , { + onRecoverableError(error) { + Scheduler.unstable_yieldValue( + 'Logged recoverable error: ' + error.message, + ); + }, + }); + Scheduler.unstable_flushAll(); + + expect(getVisibleChildren(container)).toEqual( +
+

A

+

initial

+
, + ); + + // Now that the boundary resolves to it's children the hydration completes and discovers that there is a mismatch requiring + // client-side rendering. + await clientResolve(); + expect(() => { + expect(Scheduler).toFlushAndYield([ + 'Logged recoverable error: Text content does not match server-rendered HTML.', + 'Logged recoverable error: There was an error while hydrating this Suspense boundary. Switched to client rendering.', + ]); + }).toErrorDev( + 'Warning: Prop `name` did not match. Server: "initial" Client: "replaced"', + ); + expect(getVisibleChildren(container)).toEqual( +
+

A

+

replaced

+
, + ); + + expect(Scheduler).toFlushAndYield([]); + }); + + // @gate experimental && enableClientRenderFallbackOnTextMismatch + it('only warns once on hydration mismatch while within a suspense boundary', async () => { + const originalConsoleError = console.error; + const mockError = jest.fn(); + console.error = (...args) => { + mockError(...args.map(normalizeCodeLocInfo)); + }; + + const App = ({text}) => { + return ( +
+ Loading...}> +

{text}

+

{text}

+

{text}

+
+
+ ); + }; + + try { + await act(async () => { + const {pipe} = ReactDOMFizzServer.renderToPipeableStream( + , + ); + pipe(writable); + }); + + expect(getVisibleChildren(container)).toEqual( +
+

initial

+

initial

+

initial

+
, + ); + + ReactDOMClient.hydrateRoot(container, , { + onRecoverableError(error) { + Scheduler.unstable_yieldValue( + 'Logged recoverable error: ' + error.message, + ); + }, + }); + expect(Scheduler).toFlushAndYield([ + 'Logged recoverable error: Text content does not match server-rendered HTML.', + 'Logged recoverable error: Text content does not match server-rendered HTML.', + 'Logged recoverable error: Text content does not match server-rendered HTML.', + 'Logged recoverable error: There was an error while hydrating this Suspense boundary. Switched to client rendering.', + ]); + + expect(getVisibleChildren(container)).toEqual( +
+

replaced

+

replaced

+

replaced

+
, + ); + + expect(Scheduler).toFlushAndYield([]); + if (__DEV__) { + expect(mockError.mock.calls.length).toBe(1); + expect(mockError.mock.calls[0]).toEqual([ + 'Warning: Text content did not match. Server: "%s" Client: "%s"%s', + 'initial', + 'replaced', + '\n' + + ' in h2 (at **)\n' + + ' in Suspense (at **)\n' + + ' in div (at **)\n' + + ' in App (at **)', + ]); + } else { + expect(mockError.mock.calls.length).toBe(0); + } + } finally { + console.error = originalConsoleError; + } + }); + + // @gate experimental + it('supresses hydration warnings when an error occurs within a Suspense boundary', async () => { + let isClient = false; + let shouldThrow = true; + + function ThrowUntilOnClient({children}) { + if (isClient && shouldThrow) { + throw new Error('uh oh'); + } + return children; + } + + function StopThrowingOnClient() { + if (isClient) { + shouldThrow = false; + } + return null; + } + + const App = () => { + return ( +
+ Loading...}> + +

one

+
+

two

+

{isClient ? 'five' : 'three'}

+ +
+
+ ); + }; + + await act(async () => { + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(); + pipe(writable); + }); + + expect(getVisibleChildren(container)).toEqual( +
+

one

+

two

+

three

+
, + ); + + isClient = true; + + ReactDOMClient.hydrateRoot(container, , { + onRecoverableError(error) { + Scheduler.unstable_yieldValue( + 'Logged recoverable error: ' + error.message, + ); + }, + }); + expect(Scheduler).toFlushAndYield([ + 'Logged recoverable error: uh oh', + 'Logged recoverable error: Hydration failed because the initial UI does not match what was rendered on the server.', + 'Logged recoverable error: Hydration failed because the initial UI does not match what was rendered on the server.', + 'Logged recoverable error: There was an error while hydrating this Suspense boundary. Switched to client rendering.', + ]); + + expect(getVisibleChildren(container)).toEqual( +
+

one

+

two

+

five

+
, + ); + + expect(Scheduler).toFlushAndYield([]); + }); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js b/packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js index 4fce6008bcf5..fe9d8dd453cd 100644 --- a/packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js +++ b/packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js @@ -285,7 +285,7 @@ describe('ReactDOMServerPartialHydration', () => { } try { const finalHTML = ReactDOMServer.renderToString(); - const container = document.createElement('div'); + const container = document.createElement('section'); container.innerHTML = finalHTML; expect(Scheduler).toHaveYielded([ 'Hello', @@ -350,12 +350,14 @@ describe('ReactDOMServerPartialHydration', () => { ); if (__DEV__) { - expect(mockError.mock.calls[0]).toEqual([ + const secondToLastCall = + mockError.mock.calls[mockError.mock.calls.length - 2]; + expect(secondToLastCall).toEqual([ 'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s', - 'div', - 'div', + 'article', + 'section', '\n' + - ' in div (at **)\n' + + ' in article (at **)\n' + ' in Component (at **)\n' + ' in Suspense (at **)\n' + ' in App (at **)', diff --git a/packages/react-reconciler/src/ReactFiberHydrationContext.new.js b/packages/react-reconciler/src/ReactFiberHydrationContext.new.js index 26b05d008ac5..e0e592d32790 100644 --- a/packages/react-reconciler/src/ReactFiberHydrationContext.new.js +++ b/packages/react-reconciler/src/ReactFiberHydrationContext.new.js @@ -80,7 +80,10 @@ import {queueRecoverableErrors} from './ReactFiberWorkLoop.new'; let hydrationParentFiber: null | Fiber = null; let nextHydratableInstance: null | HydratableInstance = null; let isHydrating: boolean = false; -let didSuspend: boolean = false; + +// This flag allows for warning supression when we expect there to be mismatches +// due to earlier mismatches or a suspended fiber. +let didSuspendOrErrorDEV: boolean = false; // Hydration errors that were thrown inside this boundary let hydrationErrors: Array | null = null; @@ -95,9 +98,9 @@ function warnIfHydrating() { } } -export function markDidSuspendWhileHydratingDEV() { +export function markDidThrowWhileHydratingDEV() { if (__DEV__) { - didSuspend = true; + didSuspendOrErrorDEV = true; } } @@ -113,7 +116,7 @@ function enterHydrationState(fiber: Fiber): boolean { hydrationParentFiber = fiber; isHydrating = true; hydrationErrors = null; - didSuspend = false; + didSuspendOrErrorDEV = false; return true; } @@ -131,7 +134,7 @@ function reenterHydrationStateFromDehydratedSuspenseInstance( hydrationParentFiber = fiber; isHydrating = true; hydrationErrors = null; - didSuspend = false; + didSuspendOrErrorDEV = false; if (treeContext !== null) { restoreSuspendedTreeContext(fiber, treeContext); } @@ -196,7 +199,7 @@ function deleteHydratableInstance( function warnNonhydratedInstance(returnFiber: Fiber, fiber: Fiber) { if (__DEV__) { - if (didSuspend) { + if (didSuspendOrErrorDEV) { // Inside a boundary that already suspended. We're currently rendering the // siblings of a suspended node. The mismatch may be due to the missing // data, so it's probably a false positive. @@ -444,7 +447,7 @@ function prepareToHydrateHostInstance( } const instance: Instance = fiber.stateNode; - const shouldWarnIfMismatchDev = !didSuspend; + const shouldWarnIfMismatchDev = !didSuspendOrErrorDEV; const updatePayload = hydrateInstance( instance, fiber.type, @@ -474,7 +477,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean { const textInstance: TextInstance = fiber.stateNode; const textContent: string = fiber.memoizedProps; - const shouldWarnIfMismatchDev = !didSuspend; + const shouldWarnIfMismatchDev = !didSuspendOrErrorDEV; const shouldUpdate = hydrateTextInstance( textInstance, textContent, @@ -653,7 +656,7 @@ function resetHydrationState(): void { hydrationParentFiber = null; nextHydratableInstance = null; isHydrating = false; - didSuspend = false; + didSuspendOrErrorDEV = false; } export function upgradeHydrationErrorsToRecoverable(): void { diff --git a/packages/react-reconciler/src/ReactFiberHydrationContext.old.js b/packages/react-reconciler/src/ReactFiberHydrationContext.old.js index cf7890e3edba..a4fd5c93e22e 100644 --- a/packages/react-reconciler/src/ReactFiberHydrationContext.old.js +++ b/packages/react-reconciler/src/ReactFiberHydrationContext.old.js @@ -80,7 +80,10 @@ import {queueRecoverableErrors} from './ReactFiberWorkLoop.old'; let hydrationParentFiber: null | Fiber = null; let nextHydratableInstance: null | HydratableInstance = null; let isHydrating: boolean = false; -let didSuspend: boolean = false; + +// This flag allows for warning supression when we expect there to be mismatches +// due to earlier mismatches or a suspended fiber. +let didSuspendOrErrorDEV: boolean = false; // Hydration errors that were thrown inside this boundary let hydrationErrors: Array | null = null; @@ -95,9 +98,9 @@ function warnIfHydrating() { } } -export function markDidSuspendWhileHydratingDEV() { +export function markDidThrowWhileHydratingDEV() { if (__DEV__) { - didSuspend = true; + didSuspendOrErrorDEV = true; } } @@ -113,7 +116,7 @@ function enterHydrationState(fiber: Fiber): boolean { hydrationParentFiber = fiber; isHydrating = true; hydrationErrors = null; - didSuspend = false; + didSuspendOrErrorDEV = false; return true; } @@ -131,7 +134,7 @@ function reenterHydrationStateFromDehydratedSuspenseInstance( hydrationParentFiber = fiber; isHydrating = true; hydrationErrors = null; - didSuspend = false; + didSuspendOrErrorDEV = false; if (treeContext !== null) { restoreSuspendedTreeContext(fiber, treeContext); } @@ -196,7 +199,7 @@ function deleteHydratableInstance( function warnNonhydratedInstance(returnFiber: Fiber, fiber: Fiber) { if (__DEV__) { - if (didSuspend) { + if (didSuspendOrErrorDEV) { // Inside a boundary that already suspended. We're currently rendering the // siblings of a suspended node. The mismatch may be due to the missing // data, so it's probably a false positive. @@ -444,7 +447,7 @@ function prepareToHydrateHostInstance( } const instance: Instance = fiber.stateNode; - const shouldWarnIfMismatchDev = !didSuspend; + const shouldWarnIfMismatchDev = !didSuspendOrErrorDEV; const updatePayload = hydrateInstance( instance, fiber.type, @@ -474,7 +477,7 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean { const textInstance: TextInstance = fiber.stateNode; const textContent: string = fiber.memoizedProps; - const shouldWarnIfMismatchDev = !didSuspend; + const shouldWarnIfMismatchDev = !didSuspendOrErrorDEV; const shouldUpdate = hydrateTextInstance( textInstance, textContent, @@ -653,7 +656,7 @@ function resetHydrationState(): void { hydrationParentFiber = null; nextHydratableInstance = null; isHydrating = false; - didSuspend = false; + didSuspendOrErrorDEV = false; } export function upgradeHydrationErrorsToRecoverable(): void { diff --git a/packages/react-reconciler/src/ReactFiberThrow.new.js b/packages/react-reconciler/src/ReactFiberThrow.new.js index 815cb25ef045..7db27ba935d0 100644 --- a/packages/react-reconciler/src/ReactFiberThrow.new.js +++ b/packages/react-reconciler/src/ReactFiberThrow.new.js @@ -83,7 +83,7 @@ import { } from './ReactFiberLane.new'; import { getIsHydrating, - markDidSuspendWhileHydratingDEV, + markDidThrowWhileHydratingDEV, queueHydrationError, } from './ReactFiberHydrationContext.new'; @@ -453,6 +453,12 @@ function throwException( const wakeable: Wakeable = (value: any); resetSuspendedComponent(sourceFiber, rootRenderLanes); + if (__DEV__) { + if (getIsHydrating() && sourceFiber.mode & ConcurrentMode) { + markDidThrowWhileHydratingDEV(); + } + } + if (__DEV__) { if (enableDebugTracing) { if (sourceFiber.mode & DebugTracingMode) { @@ -514,8 +520,7 @@ function throwException( } else { // This is a regular error, not a Suspense wakeable. if (getIsHydrating() && sourceFiber.mode & ConcurrentMode) { - markDidSuspendWhileHydratingDEV(); - + markDidThrowWhileHydratingDEV(); const suspenseBoundary = getNearestSuspenseBoundaryToCapture(returnFiber); // If the error was thrown during hydration, we may be able to recover by // discarding the dehydrated content and switching to a client render. diff --git a/packages/react-reconciler/src/ReactFiberThrow.old.js b/packages/react-reconciler/src/ReactFiberThrow.old.js index ec89f5ab0cd5..a8e75e9ce661 100644 --- a/packages/react-reconciler/src/ReactFiberThrow.old.js +++ b/packages/react-reconciler/src/ReactFiberThrow.old.js @@ -83,7 +83,7 @@ import { } from './ReactFiberLane.old'; import { getIsHydrating, - markDidSuspendWhileHydratingDEV, + markDidThrowWhileHydratingDEV, queueHydrationError, } from './ReactFiberHydrationContext.old'; @@ -453,6 +453,12 @@ function throwException( const wakeable: Wakeable = (value: any); resetSuspendedComponent(sourceFiber, rootRenderLanes); + if (__DEV__) { + if (getIsHydrating() && sourceFiber.mode & ConcurrentMode) { + markDidThrowWhileHydratingDEV(); + } + } + if (__DEV__) { if (enableDebugTracing) { if (sourceFiber.mode & DebugTracingMode) { @@ -514,8 +520,7 @@ function throwException( } else { // This is a regular error, not a Suspense wakeable. if (getIsHydrating() && sourceFiber.mode & ConcurrentMode) { - markDidSuspendWhileHydratingDEV(); - + markDidThrowWhileHydratingDEV(); const suspenseBoundary = getNearestSuspenseBoundaryToCapture(returnFiber); // If the error was thrown during hydration, we may be able to recover by // discarding the dehydrated content and switching to a client render.