GeezerVison is showing github.com — Fix false positive hydration warnings (#23364) · react/react@f468816
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +66Lines changed: 66 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -301,6 +301,72 @@ describe('ReactDOMFizzServer', () => { | 301 | 301 | ); | 302 | 302 | }); | 303 | 303 |
| | 304 | + // @gate experimental | | 305 | + it('#23331: does not warn about hydration mismatches if something suspended in an earlier sibling', async () => { | | 306 | + const makeApp = () => { | | 307 | + let resolve; | | 308 | + const imports = new Promise(r => { | | 309 | + resolve = () => r({default: () => <span id="async">async</span>}); | | 310 | + }); | | 311 | + const Lazy = React.lazy(() => imports); | | 312 | + | | 313 | + const App = () => ( | | 314 | + <div> | | 315 | + <Suspense fallback={<span>Loading...</span>}> | | 316 | + <Lazy /> | | 317 | + <span id="after">after</span> | | 318 | + </Suspense> | | 319 | + </div> | | 320 | + ); | | 321 | + | | 322 | + return [App, resolve]; | | 323 | + }; | | 324 | + | | 325 | + // Server-side | | 326 | + const [App, resolve] = makeApp(); | | 327 | + await act(async () => { | | 328 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | | 329 | + pipe(writable); | | 330 | + }); | | 331 | + expect(getVisibleChildren(container)).toEqual( | | 332 | + <div> | | 333 | + <span>Loading...</span> | | 334 | + </div>, | | 335 | + ); | | 336 | + await act(async () => { | | 337 | + resolve(); | | 338 | + }); | | 339 | + expect(getVisibleChildren(container)).toEqual( | | 340 | + <div> | | 341 | + <span id="async">async</span> | | 342 | + <span id="after">after</span> | | 343 | + </div>, | | 344 | + ); | | 345 | + | | 346 | + // Client-side | | 347 | + const [HydrateApp, hydrateResolve] = makeApp(); | | 348 | + await act(async () => { | | 349 | + ReactDOM.hydrateRoot(container, <HydrateApp />); | | 350 | + }); | | 351 | + | | 352 | + expect(getVisibleChildren(container)).toEqual( | | 353 | + <div> | | 354 | + <span id="async">async</span> | | 355 | + <span id="after">after</span> | | 356 | + </div>, | | 357 | + ); | | 358 | + | | 359 | + await act(async () => { | | 360 | + hydrateResolve(); | | 361 | + }); | | 362 | + expect(getVisibleChildren(container)).toEqual( | | 363 | + <div> | | 364 | + <span id="async">async</span> | | 365 | + <span id="after">after</span> | | 366 | + </div>, | | 367 | + ); | | 368 | + }); | | 369 | + | 304 | 370 | // @gate experimental | 305 | 371 | it('should support nonce scripts', async () => { | 306 | 372 | CSPnonce = 'R4nd0m'; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMComponent.js +20-11Lines changed: 20 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -230,21 +230,24 @@ export function checkForUnmatchedText( | 230 | 230 | serverText: string, | 231 | 231 | clientText: string | number, | 232 | 232 | isConcurrentMode: boolean, | | 233 | + shouldWarnDev: boolean, | 233 | 234 | ) { | 234 | 235 | const normalizedClientText = normalizeMarkupForTextOrAttribute(clientText); | 235 | 236 | const normalizedServerText = normalizeMarkupForTextOrAttribute(serverText); | 236 | 237 | if (normalizedServerText === normalizedClientText) { | 237 | 238 | return; | 238 | 239 | } | 239 | 240 |
| 240 | | - if (__DEV__) { | 241 | | - if (!didWarnInvalidHydration) { | 242 | | - didWarnInvalidHydration = true; | 243 | | - console.error( | 244 | | - 'Text content did not match. Server: "%s" Client: "%s"', | 245 | | - normalizedServerText, | 246 | | - normalizedClientText, | 247 | | - ); | | 241 | + if (shouldWarnDev) { | | 242 | + if (__DEV__) { | | 243 | + if (!didWarnInvalidHydration) { | | 244 | + didWarnInvalidHydration = true; | | 245 | + console.error( | | 246 | + 'Text content did not match. Server: "%s" Client: "%s"', | | 247 | + normalizedServerText, | | 248 | + normalizedClientText, | | 249 | + ); | | 250 | + } | 248 | 251 | } | 249 | 252 | } | 250 | 253 |
| @@ -866,6 +869,7 @@ export function diffHydratedProperties( | 866 | 869 | parentNamespace: string, | 867 | 870 | rootContainerElement: Element | Document, | 868 | 871 | isConcurrentMode: boolean, | | 872 | + shouldWarnDev: boolean, | 869 | 873 | ): null | Array<mixed> { | 870 | 874 | let isCustomComponentTag; | 871 | 875 | let extraAttributeNames: Set<string>; | @@ -985,6 +989,7 @@ export function diffHydratedProperties( | 985 | 989 | domElement.textContent, | 986 | 990 | nextProp, | 987 | 991 | isConcurrentMode, | | 992 | + shouldWarnDev, | 988 | 993 | ); | 989 | 994 | } | 990 | 995 | updatePayload = [CHILDREN, nextProp]; | @@ -996,6 +1001,7 @@ export function diffHydratedProperties( | 996 | 1001 | domElement.textContent, | 997 | 1002 | nextProp, | 998 | 1003 | isConcurrentMode, | | 1004 | + shouldWarnDev, | 999 | 1005 | ); | 1000 | 1006 | } | 1001 | 1007 | updatePayload = [CHILDREN, '' + nextProp]; | @@ -1011,6 +1017,7 @@ export function diffHydratedProperties( | 1011 | 1017 | } | 1012 | 1018 | } | 1013 | 1019 | } else if ( | | 1020 | + shouldWarnDev && | 1014 | 1021 | __DEV__ && | 1015 | 1022 | // Convince Flow we've calculated it (it's DEV-only in this method.) | 1016 | 1023 | typeof isCustomComponentTag === 'boolean' | @@ -1142,10 +1149,12 @@ export function diffHydratedProperties( | 1142 | 1149 | } | 1143 | 1150 |
| 1144 | 1151 | if (__DEV__) { | 1145 | | - // $FlowFixMe - Should be inferred as not undefined. | 1146 | | - if (extraAttributeNames.size > 0 && !suppressHydrationWarning) { | | 1152 | + if (shouldWarnDev) { | 1147 | 1153 | // $FlowFixMe - Should be inferred as not undefined. | 1148 | | - warnForExtraAttributes(extraAttributeNames); | | 1154 | + if (extraAttributeNames.size > 0 && !suppressHydrationWarning) { | | 1155 | + // $FlowFixMe - Should be inferred as not undefined. | | 1156 | + warnForExtraAttributes(extraAttributeNames); | | 1157 | + } | 1149 | 1158 | } | 1150 | 1159 | } | 1151 | 1160 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMHostConfig.js +17-2Lines changed: 17 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -786,6 +786,7 @@ export function hydrateInstance( | 786 | 786 | rootContainerInstance: Container, | 787 | 787 | hostContext: HostContext, | 788 | 788 | internalInstanceHandle: Object, | | 789 | + shouldWarnDev: boolean, | 789 | 790 | ): null | Array<mixed> { | 790 | 791 | precacheFiberNode(internalInstanceHandle, instance); | 791 | 792 | // TODO: Possibly defer this until the commit phase where all the events | @@ -811,13 +812,15 @@ export function hydrateInstance( | 811 | 812 | parentNamespace, | 812 | 813 | rootContainerInstance, | 813 | 814 | isConcurrentMode, | | 815 | + shouldWarnDev, | 814 | 816 | ); | 815 | 817 | } | 816 | 818 |
| 817 | 819 | export function hydrateTextInstance( | 818 | 820 | textInstance: TextInstance, | 819 | 821 | text: string, | 820 | 822 | internalInstanceHandle: Object, | | 823 | + shouldWarnDev: boolean, | 821 | 824 | ): boolean { | 822 | 825 | precacheFiberNode(internalInstanceHandle, textInstance); | 823 | 826 |
| @@ -924,7 +927,13 @@ export function didNotMatchHydratedContainerTextInstance( | 924 | 927 | text: string, | 925 | 928 | isConcurrentMode: boolean, | 926 | 929 | ) { | 927 | | - checkForUnmatchedText(textInstance.nodeValue, text, isConcurrentMode); | | 930 | + const shouldWarnDev = true; | | 931 | + checkForUnmatchedText( | | 932 | + textInstance.nodeValue, | | 933 | + text, | | 934 | + isConcurrentMode, | | 935 | + shouldWarnDev, | | 936 | + ); | 928 | 937 | } | 929 | 938 |
| 930 | 939 | export function didNotMatchHydratedTextInstance( | @@ -936,7 +945,13 @@ export function didNotMatchHydratedTextInstance( | 936 | 945 | isConcurrentMode: boolean, | 937 | 946 | ) { | 938 | 947 | if (parentProps[SUPPRESS_HYDRATION_WARNING] !== true) { | 939 | | - checkForUnmatchedText(textInstance.nodeValue, text, isConcurrentMode); | | 948 | + const shouldWarnDev = true; | | 949 | + checkForUnmatchedText( | | 950 | + textInstance.nodeValue, | | 951 | + text, | | 952 | + isConcurrentMode, | | 953 | + shouldWarnDev, | | 954 | + ); | 940 | 955 | } | 941 | 956 | } | 942 | 957 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.new.js +26-1Lines changed: 26 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,6 +84,7 @@ import {queueRecoverableErrors} from './ReactFiberWorkLoop.new'; | 84 | 84 | let hydrationParentFiber: null | Fiber = null; | 85 | 85 | let nextHydratableInstance: null | HydratableInstance = null; | 86 | 86 | let isHydrating: boolean = false; | | 87 | +let didSuspend: boolean = false; | 87 | 88 |
| 88 | 89 | // Hydration errors that were thrown inside this boundary | 89 | 90 | let hydrationErrors: Array<mixed> | null = null; | @@ -98,6 +99,12 @@ function warnIfHydrating() { | 98 | 99 | } | 99 | 100 | } | 100 | 101 |
| | 102 | +export function markDidSuspendWhileHydratingDEV() { | | 103 | + if (__DEV__) { | | 104 | + didSuspend = true; | | 105 | + } | | 106 | +} | | 107 | + | 101 | 108 | function enterHydrationState(fiber: Fiber): boolean { | 102 | 109 | if (!supportsHydration) { | 103 | 110 | return false; | @@ -110,6 +117,7 @@ function enterHydrationState(fiber: Fiber): boolean { | 110 | 117 | hydrationParentFiber = fiber; | 111 | 118 | isHydrating = true; | 112 | 119 | hydrationErrors = null; | | 120 | + didSuspend = false; | 113 | 121 | return true; | 114 | 122 | } | 115 | 123 |
| @@ -127,6 +135,7 @@ function reenterHydrationStateFromDehydratedSuspenseInstance( | 127 | 135 | hydrationParentFiber = fiber; | 128 | 136 | isHydrating = true; | 129 | 137 | hydrationErrors = null; | | 138 | + didSuspend = false; | 130 | 139 | if (treeContext !== null) { | 131 | 140 | restoreSuspendedTreeContext(fiber, treeContext); | 132 | 141 | } | @@ -185,6 +194,13 @@ function deleteHydratableInstance( | 185 | 194 |
| 186 | 195 | function warnNonhydratedInstance(returnFiber: Fiber, fiber: Fiber) { | 187 | 196 | if (__DEV__) { | | 197 | + if (didSuspend) { | | 198 | + // Inside a boundary that already suspended. We're currently rendering the | | 199 | + // siblings of a suspended node. The mismatch may be due to the missing | | 200 | + // data, so it's probably a false positive. | | 201 | + return; | | 202 | + } | | 203 | + | 188 | 204 | switch (returnFiber.tag) { | 189 | 205 | case HostRoot: { | 190 | 206 | const parentContainer = returnFiber.stateNode.containerInfo; | @@ -418,13 +434,15 @@ function prepareToHydrateHostInstance( | 418 | 434 | } | 419 | 435 |
| 420 | 436 | const instance: Instance = fiber.stateNode; | | 437 | + const shouldWarnIfMismatchDev = !didSuspend; | 421 | 438 | const updatePayload = hydrateInstance( | 422 | 439 | instance, | 423 | 440 | fiber.type, | 424 | 441 | fiber.memoizedProps, | 425 | 442 | rootContainerInstance, | 426 | 443 | hostContext, | 427 | 444 | fiber, | | 445 | + shouldWarnIfMismatchDev, | 428 | 446 | ); | 429 | 447 | // TODO: Type this specific to this type of component. | 430 | 448 | fiber.updateQueue = (updatePayload: any); | @@ -446,7 +464,13 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean { | 446 | 464 |
| 447 | 465 | const textInstance: TextInstance = fiber.stateNode; | 448 | 466 | const textContent: string = fiber.memoizedProps; | 449 | | - const shouldUpdate = hydrateTextInstance(textInstance, textContent, fiber); | | 467 | + const shouldWarnIfMismatchDev = !didSuspend; | | 468 | + const shouldUpdate = hydrateTextInstance( | | 469 | + textInstance, | | 470 | + textContent, | | 471 | + fiber, | | 472 | + shouldWarnIfMismatchDev, | | 473 | + ); | 450 | 474 | if (shouldUpdate) { | 451 | 475 | // We assume that prepareToHydrateHostTextInstance is called in a context where the | 452 | 476 | // hydration parent is the parent host component of this host text. | @@ -616,6 +640,7 @@ function resetHydrationState(): void { | 616 | 640 | hydrationParentFiber = null; | 617 | 641 | nextHydratableInstance = null; | 618 | 642 | isHydrating = false; | | 643 | + didSuspend = false; | 619 | 644 | } | 620 | 645 |
| 621 | 646 | export function upgradeHydrationErrorsToRecoverable(): void { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.old.js +26-1Lines changed: 26 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,6 +84,7 @@ import {queueRecoverableErrors} from './ReactFiberWorkLoop.old'; | 84 | 84 | let hydrationParentFiber: null | Fiber = null; | 85 | 85 | let nextHydratableInstance: null | HydratableInstance = null; | 86 | 86 | let isHydrating: boolean = false; | | 87 | +let didSuspend: boolean = false; | 87 | 88 |
| 88 | 89 | // Hydration errors that were thrown inside this boundary | 89 | 90 | let hydrationErrors: Array<mixed> | null = null; | @@ -98,6 +99,12 @@ function warnIfHydrating() { | 98 | 99 | } | 99 | 100 | } | 100 | 101 |
| | 102 | +export function markDidSuspendWhileHydratingDEV() { | | 103 | + if (__DEV__) { | | 104 | + didSuspend = true; | | 105 | + } | | 106 | +} | | 107 | + | 101 | 108 | function enterHydrationState(fiber: Fiber): boolean { | 102 | 109 | if (!supportsHydration) { | 103 | 110 | return false; | @@ -110,6 +117,7 @@ function enterHydrationState(fiber: Fiber): boolean { | 110 | 117 | hydrationParentFiber = fiber; | 111 | 118 | isHydrating = true; | 112 | 119 | hydrationErrors = null; | | 120 | + didSuspend = false; | 113 | 121 | return true; | 114 | 122 | } | 115 | 123 |
| @@ -127,6 +135,7 @@ function reenterHydrationStateFromDehydratedSuspenseInstance( | 127 | 135 | hydrationParentFiber = fiber; | 128 | 136 | isHydrating = true; | 129 | 137 | hydrationErrors = null; | | 138 | + didSuspend = false; | 130 | 139 | if (treeContext !== null) { | 131 | 140 | restoreSuspendedTreeContext(fiber, treeContext); | 132 | 141 | } | @@ -185,6 +194,13 @@ function deleteHydratableInstance( | 185 | 194 |
| 186 | 195 | function warnNonhydratedInstance(returnFiber: Fiber, fiber: Fiber) { | 187 | 196 | if (__DEV__) { | | 197 | + if (didSuspend) { | | 198 | + // Inside a boundary that already suspended. We're currently rendering the | | 199 | + // siblings of a suspended node. The mismatch may be due to the missing | | 200 | + // data, so it's probably a false positive. | | 201 | + return; | | 202 | + } | | 203 | + | 188 | 204 | switch (returnFiber.tag) { | 189 | 205 | case HostRoot: { | 190 | 206 | const parentContainer = returnFiber.stateNode.containerInfo; | @@ -418,13 +434,15 @@ function prepareToHydrateHostInstance( | 418 | 434 | } | 419 | 435 |
| 420 | 436 | const instance: Instance = fiber.stateNode; | | 437 | + const shouldWarnIfMismatchDev = !didSuspend; | 421 | 438 | const updatePayload = hydrateInstance( | 422 | 439 | instance, | 423 | 440 | fiber.type, | 424 | 441 | fiber.memoizedProps, | 425 | 442 | rootContainerInstance, | 426 | 443 | hostContext, | 427 | 444 | fiber, | | 445 | + shouldWarnIfMismatchDev, | 428 | 446 | ); | 429 | 447 | // TODO: Type this specific to this type of component. | 430 | 448 | fiber.updateQueue = (updatePayload: any); | @@ -446,7 +464,13 @@ function prepareToHydrateHostTextInstance(fiber: Fiber): boolean { | 446 | 464 |
| 447 | 465 | const textInstance: TextInstance = fiber.stateNode; | 448 | 466 | const textContent: string = fiber.memoizedProps; | 449 | | - const shouldUpdate = hydrateTextInstance(textInstance, textContent, fiber); | | 467 | + const shouldWarnIfMismatchDev = !didSuspend; | | 468 | + const shouldUpdate = hydrateTextInstance( | | 469 | + textInstance, | | 470 | + textContent, | | 471 | + fiber, | | 472 | + shouldWarnIfMismatchDev, | | 473 | + ); | 450 | 474 | if (shouldUpdate) { | 451 | 475 | // We assume that prepareToHydrateHostTextInstance is called in a context where the | 452 | 476 | // hydration parent is the parent host component of this host text. | @@ -616,6 +640,7 @@ function resetHydrationState(): void { | 616 | 640 | hydrationParentFiber = null; | 617 | 641 | nextHydratableInstance = null; | 618 | 642 | isHydrating = false; | | 643 | + didSuspend = false; | 619 | 644 | } | 620 | 645 |
| 621 | 646 | export function upgradeHydrationErrorsToRecoverable(): void { | |
|