GeezerVison is showing github.com — Don't delete trailing mismatches during hydration at the root (#21021) · react/react@825c302
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 +13-16Lines changed: 13 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,7 @@ let Suspense; | 19 | 19 | let textCache; | 20 | 20 | let document; | 21 | 21 | let writable; | | 22 | +let container; | 22 | 23 | let buffer = ''; | 23 | 24 | let hasErrored = false; | 24 | 25 | let fatalError = undefined; | @@ -38,10 +39,14 @@ describe('ReactDOMFizzServer', () => { | 38 | 39 | textCache = new Map(); | 39 | 40 |
| 40 | 41 | // Test Environment | 41 | | - const jsdom = new JSDOM('<!DOCTYPE html><html><head></head><body>', { | 42 | | - runScripts: 'dangerously', | 43 | | - }); | | 42 | + const jsdom = new JSDOM( | | 43 | + '<!DOCTYPE html><html><head></head><body><div id="container">', | | 44 | + { | | 45 | + runScripts: 'dangerously', | | 46 | + }, | | 47 | + ); | 44 | 48 | document = jsdom.window.document; | | 49 | + container = document.getElementById('container'); | 45 | 50 |
| 46 | 51 | buffer = ''; | 47 | 52 | hasErrored = false; | @@ -80,9 +85,9 @@ describe('ReactDOMFizzServer', () => { | 80 | 85 | const script = document.createElement('script'); | 81 | 86 | script.textContent = node.textContent; | 82 | 87 | fakeBody.removeChild(node); | 83 | | - document.body.appendChild(script); | | 88 | + container.appendChild(script); | 84 | 89 | } else { | 85 | | - document.body.appendChild(node); | | 90 | + container.appendChild(node); | 86 | 91 | } | 87 | 92 | } | 88 | 93 | } | @@ -200,11 +205,11 @@ describe('ReactDOMFizzServer', () => { | 200 | 205 | writable, | 201 | 206 | ); | 202 | 207 | }); | 203 | | - expect(getVisibleChildren(document.body)).toEqual(<div>Loading...</div>); | | 208 | + expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>); | 204 | 209 | await act(async () => { | 205 | 210 | resolveText('Hello World'); | 206 | 211 | }); | 207 | | - expect(getVisibleChildren(document.body)).toEqual(<div>Hello World</div>); | | 212 | + expect(getVisibleChildren(container)).toEqual(<div>Hello World</div>); | 208 | 213 | }); | 209 | 214 |
| 210 | 215 | // @gate experimental | @@ -224,20 +229,12 @@ describe('ReactDOMFizzServer', () => { | 224 | 229 | } | 225 | 230 |
| 226 | 231 | await act(async () => { | 227 | | - ReactDOMFizzServer.pipeToNodeWritable( | 228 | | - // We currently have to wrap the server node in a container because | 229 | | - // otherwise the Fizz nodes get deleted during hydration. | 230 | | - <div id="container"> | 231 | | - <App /> | 232 | | - </div>, | 233 | | - writable, | 234 | | - ); | | 232 | + ReactDOMFizzServer.pipeToNodeWritable(<App />, writable); | 235 | 233 | }); | 236 | 234 |
| 237 | 235 | // We're still showing a fallback. | 238 | 236 |
| 239 | 237 | // Attempt to hydrate the content. | 240 | | - const container = document.body.firstChild; | 241 | 238 | const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | 242 | 239 | root.render(<App />); | 243 | 240 | Scheduler.unstable_flushAll(); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerIntegrationFragment-test.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,7 +103,15 @@ describe('ReactDOMServerIntegration', () => { | 103 | 103 | }); | 104 | 104 |
| 105 | 105 | itRenders('an empty fragment', async render => { | 106 | | - expect(await render(<React.Fragment />)).toBe(null); | | 106 | + expect( | | 107 | + ( | | 108 | + await render( | | 109 | + <div> | | 110 | + <React.Fragment /> | | 111 | + </div>, | | 112 | + ) | | 113 | + ).firstChild, | | 114 | + ).toBe(null); | 107 | 115 | }); | 108 | 116 | }); | 109 | 117 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.js +18-10Lines changed: 18 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1654,9 +1654,13 @@ describe('ReactDOMServerHooks', () => { | 1654 | 1654 | // This is the wrong HTML string | 1655 | 1655 | container.innerHTML = '<span></span>'; | 1656 | 1656 | ReactDOM.unstable_createRoot(container, {hydrate: true}).render(<App />); | 1657 | | - expect(() => Scheduler.unstable_flushAll()).toErrorDev([ | 1658 | | - 'Warning: Expected server HTML to contain a matching <div> in <div>.', | 1659 | | - ]); | | 1657 | + expect(() => Scheduler.unstable_flushAll()).toErrorDev( | | 1658 | + [ | | 1659 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | | 1660 | + 'Warning: Expected server HTML to contain a matching <div> in <div>.', | | 1661 | + ], | | 1662 | + {withoutStack: 1}, | | 1663 | + ); | 1660 | 1664 | }); | 1661 | 1665 |
| 1662 | 1666 | // @gate experimental | @@ -1740,9 +1744,13 @@ describe('ReactDOMServerHooks', () => { | 1740 | 1744 | // This is the wrong HTML string | 1741 | 1745 | container.innerHTML = '<span></span>'; | 1742 | 1746 | ReactDOM.unstable_createRoot(container, {hydrate: true}).render(<App />); | 1743 | | - expect(() => Scheduler.unstable_flushAll()).toErrorDev([ | 1744 | | - 'Warning: Expected server HTML to contain a matching <div> in <div>.', | 1745 | | - ]); | | 1747 | + expect(() => Scheduler.unstable_flushAll()).toErrorDev( | | 1748 | + [ | | 1749 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | | 1750 | + 'Warning: Expected server HTML to contain a matching <div> in <div>.', | | 1751 | + ], | | 1752 | + {withoutStack: 1}, | | 1753 | + ); | 1746 | 1754 | }); | 1747 | 1755 |
| 1748 | 1756 | // @gate experimental | @@ -1764,7 +1772,7 @@ describe('ReactDOMServerHooks', () => { | 1764 | 1772 | expect(() => Scheduler.unstable_flushAll()).toErrorDev( | 1765 | 1773 | [ | 1766 | 1774 | 'Warning: The object passed back from useOpaqueIdentifier is meant to be passed through to attributes only. Do not read the value directly.', | 1767 | | - 'Warning: Did not expect server HTML to contain a <span> in <div>.', | | 1775 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | 1768 | 1776 | ], | 1769 | 1777 | {withoutStack: 1}, | 1770 | 1778 | ); | @@ -1789,7 +1797,7 @@ describe('ReactDOMServerHooks', () => { | 1789 | 1797 | expect(() => Scheduler.unstable_flushAll()).toErrorDev( | 1790 | 1798 | [ | 1791 | 1799 | 'Warning: The object passed back from useOpaqueIdentifier is meant to be passed through to attributes only. Do not read the value directly.', | 1792 | | - 'Warning: Did not expect server HTML to contain a <span> in <div>.', | | 1800 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | 1793 | 1801 | ], | 1794 | 1802 | {withoutStack: 1}, | 1795 | 1803 | ); | @@ -1813,7 +1821,7 @@ describe('ReactDOMServerHooks', () => { | 1813 | 1821 | expect(() => Scheduler.unstable_flushAll()).toErrorDev( | 1814 | 1822 | [ | 1815 | 1823 | 'Warning: The object passed back from useOpaqueIdentifier is meant to be passed through to attributes only. Do not read the value directly.', | 1816 | | - 'Warning: Did not expect server HTML to contain a <div> in <div>.', | | 1824 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | 1817 | 1825 | ], | 1818 | 1826 | {withoutStack: 1}, | 1819 | 1827 | ); | @@ -1834,7 +1842,7 @@ describe('ReactDOMServerHooks', () => { | 1834 | 1842 | expect(() => Scheduler.unstable_flushAll()).toErrorDev( | 1835 | 1843 | [ | 1836 | 1844 | 'Warning: The object passed back from useOpaqueIdentifier is meant to be passed through to attributes only. Do not read the value directly.', | 1837 | | - 'Warning: Did not expect server HTML to contain a <div> in <div>.', | | 1845 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.', | 1838 | 1846 | ], | 1839 | 1847 | {withoutStack: 1}, | 1840 | 1848 | ); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerIntegrationModes-test.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,7 +153,15 @@ describe('ReactDOMServerIntegration', () => { | 153 | 153 | }); | 154 | 154 |
| 155 | 155 | itRenders('an empty strict mode', async render => { | 156 | | - expect(await render(<React.StrictMode />)).toBe(null); | | 156 | + expect( | | 157 | + ( | | 158 | + await render( | | 159 | + <div> | | 160 | + <React.StrictMode /> | | 161 | + </div>, | | 162 | + ) | | 163 | + ).firstChild, | | 164 | + ).toBe(null); | 157 | 165 | }); | 158 | 166 | }); | 159 | 167 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactMount-test.js +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,16 +123,12 @@ describe('ReactMount', () => { | 123 | 123 | expect(instance1 === instance2).toBe(true); | 124 | 124 | }); | 125 | 125 |
| 126 | | - it('should warn if mounting into left padded rendered markup', () => { | | 126 | + it('does not warn if mounting into left padded rendered markup', () => { | 127 | 127 | const container = document.createElement('container'); | 128 | 128 | container.innerHTML = ReactDOMServer.renderToString(<div />) + ' '; | 129 | 129 |
| 130 | | - expect(() => | 131 | | - ReactDOM.hydrate(<div />, container), | 132 | | - ).toErrorDev( | 133 | | - 'Did not expect server HTML to contain the text node " " in <container>.', | 134 | | - {withoutStack: true}, | 135 | | - ); | | 130 | + // This should probably ideally warn but we ignore extra markup at the root. | | 131 | + ReactDOM.hydrate(<div />, container); | 136 | 132 | }); | 137 | 133 |
| 138 | 134 | it('should warn if mounting into right padded rendered markup', () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactRenderDocument-test.js +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -368,12 +368,31 @@ describe('rendering React components at document', () => { | 368 | 368 | expect(testDocument.body.innerHTML).toBe('Hello world'); | 369 | 369 | }); | 370 | 370 |
| 371 | | - it('renders over an existing text child without throwing', () => { | | 371 | + it('cannot render over an existing text child at the root', () => { | 372 | 372 | const container = document.createElement('div'); | 373 | 373 | container.textContent = 'potato'; | 374 | 374 | expect(() => ReactDOM.hydrate(<div>parsnip</div>, container)).toErrorDev( | 375 | 375 | 'Expected server HTML to contain a matching <div> in <div>.', | 376 | 376 | ); | | 377 | + // This creates an unfortunate double text case. | | 378 | + expect(container.textContent).toBe('potatoparsnip'); | | 379 | + }); | | 380 | + | | 381 | + it('renders over an existing nested text child without throwing', () => { | | 382 | + const container = document.createElement('div'); | | 383 | + const wrapper = document.createElement('div'); | | 384 | + wrapper.textContent = 'potato'; | | 385 | + container.appendChild(wrapper); | | 386 | + expect(() => | | 387 | + ReactDOM.hydrate( | | 388 | + <div> | | 389 | + <div>parsnip</div> | | 390 | + </div>, | | 391 | + container, | | 392 | + ), | | 393 | + ).toErrorDev( | | 394 | + 'Expected server HTML to contain a matching <div> in <div>.', | | 395 | + ); | 377 | 396 | expect(container.textContent).toBe('parsnip'); | 378 | 397 | }); | 379 | 398 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactServerRenderingHydration-test.js +28-7Lines changed: 28 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -510,26 +510,47 @@ describe('ReactDOMServerHydration', () => { | 510 | 510 |
| 511 | 511 | it('Suspense + hydration in legacy mode', () => { | 512 | 512 | const element = document.createElement('div'); | 513 | | - element.innerHTML = '<div>Hello World</div>'; | 514 | | - const div = element.firstChild; | | 513 | + element.innerHTML = '<div><div>Hello World</div></div>'; | | 514 | + const div = element.firstChild.firstChild; | 515 | 515 | const ref = React.createRef(); | 516 | 516 | expect(() => | 517 | 517 | ReactDOM.hydrate( | 518 | | - <React.Suspense fallback={null}> | 519 | | - <div ref={ref}>Hello World</div> | 520 | | - </React.Suspense>, | | 518 | + <div> | | 519 | + <React.Suspense fallback={null}> | | 520 | + <div ref={ref}>Hello World</div> | | 521 | + </React.Suspense> | | 522 | + </div>, | 521 | 523 | element, | 522 | 524 | ), | 523 | 525 | ).toErrorDev( | 524 | 526 | 'Warning: Did not expect server HTML to contain a <div> in <div>.', | 525 | | - {withoutStack: true}, | 526 | 527 | ); | 527 | 528 |
| 528 | 529 | // The content should've been client rendered and replaced the | 529 | 530 | // existing div. | 530 | 531 | expect(ref.current).not.toBe(div); | 531 | 532 | // The HTML should be the same though. | 532 | | - expect(element.innerHTML).toBe('<div>Hello World</div>'); | | 533 | + expect(element.innerHTML).toBe('<div><div>Hello World</div></div>'); | | 534 | + }); | | 535 | + | | 536 | + it('Suspense + hydration in legacy mode (at root)', () => { | | 537 | + const element = document.createElement('div'); | | 538 | + element.innerHTML = '<div>Hello World</div>'; | | 539 | + const div = element.firstChild; | | 540 | + const ref = React.createRef(); | | 541 | + ReactDOM.hydrate( | | 542 | + <React.Suspense fallback={null}> | | 543 | + <div ref={ref}>Hello World</div> | | 544 | + </React.Suspense>, | | 545 | + element, | | 546 | + ); | | 547 | + | | 548 | + // The content should've been client rendered. | | 549 | + expect(ref.current).not.toBe(div); | | 550 | + // Unfortunately, since we don't delete the tail at the root, a duplicate will remain. | | 551 | + expect(element.innerHTML).toBe( | | 552 | + '<div>Hello World</div><div>Hello World</div>', | | 553 | + ); | 533 | 554 | }); | 534 | 555 |
| 535 | 556 | it('Suspense + hydration in legacy mode with no fallback', () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMHostConfig.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -894,6 +894,12 @@ export function commitHydratedSuspenseInstance( | 894 | 894 | retryIfBlockedOn(suspenseInstance); | 895 | 895 | } | 896 | 896 |
| | 897 | +export function shouldDeleteUnhydratedTailInstances( | | 898 | + parentType: string, | | 899 | +): boolean { | | 900 | + return parentType !== 'head' || parentType !== 'body'; | | 901 | +} | | 902 | + | 897 | 903 | export function didNotMatchHydratedContainerTextInstance( | 898 | 904 | parentContainer: Container, | 899 | 905 | textInstance: TextInstance, | @@ -1008,6 +1014,15 @@ export function didNotFindHydratableSuspenseInstance( | 1008 | 1014 | } | 1009 | 1015 | } | 1010 | 1016 |
| | 1017 | +export function errorHydratingContainer(parentContainer: Container): void { | | 1018 | + if (__DEV__) { | | 1019 | + console.error( | | 1020 | + 'An error occurred during hydration. The server HTML was replaced with client content in <%s>.', | | 1021 | + parentContainer.nodeName.toLowerCase(), | | 1022 | + ); | | 1023 | + } | | 1024 | +} | | 1025 | + | 1011 | 1026 | export function getInstanceFromNode(node: HTMLElement): null | Object { | 1012 | 1027 | return getClosestInstanceFromNode(node) || null; | 1013 | 1028 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHostConfigWithNoHydration.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,7 @@ export const commitHydratedContainer = shim; | 40 | 40 | export const commitHydratedSuspenseInstance = shim; | 41 | 41 | export const clearSuspenseBoundary = shim; | 42 | 42 | export const clearSuspenseBoundaryFromContainer = shim; | | 43 | +export const shouldDeleteUnhydratedTailInstances = shim; | 43 | 44 | export const didNotMatchHydratedContainerTextInstance = shim; | 44 | 45 | export const didNotMatchHydratedTextInstance = shim; | 45 | 46 | export const didNotHydrateContainerInstance = shim; | @@ -50,3 +51,4 @@ export const didNotFindHydratableContainerSuspenseInstance = shim; | 50 | 51 | export const didNotFindHydratableInstance = shim; | 51 | 52 | export const didNotFindHydratableTextInstance = shim; | 52 | 53 | export const didNotFindHydratableSuspenseInstance = shim; | | 54 | +export const errorHydratingContainer = shim; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.new.js +6-8Lines changed: 6 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ import { | 43 | 43 | hydrateTextInstance, | 44 | 44 | hydrateSuspenseInstance, | 45 | 45 | getNextHydratableInstanceAfterSuspenseInstance, | | 46 | + shouldDeleteUnhydratedTailInstances, | 46 | 47 | didNotMatchHydratedContainerTextInstance, | 47 | 48 | didNotMatchHydratedTextInstance, | 48 | 49 | didNotHydrateContainerInstance, | @@ -438,18 +439,15 @@ function popHydrationState(fiber: Fiber): boolean { | 438 | 439 | return false; | 439 | 440 | } | 440 | 441 |
| 441 | | - const type = fiber.type; | 442 | | - | 443 | 442 | // If we have any remaining hydratable nodes, we need to delete them now. | 444 | 443 | // We only do this deeper than head and body since they tend to have random | 445 | 444 | // other nodes in them. We also ignore components with pure text content in | 446 | | - // side of them. | 447 | | - // TODO: Better heuristic. | | 445 | + // side of them. We also don't delete anything inside the root container. | 448 | 446 | if ( | 449 | | - fiber.tag !== HostComponent || | 450 | | - (type !== 'head' && | 451 | | - type !== 'body' && | 452 | | - !shouldSetTextContent(type, fiber.memoizedProps)) | | 447 | + fiber.tag !== HostRoot && | | 448 | + (fiber.tag !== HostComponent || | | 449 | + (shouldDeleteUnhydratedTailInstances(fiber.type) && | | 450 | + !shouldSetTextContent(fiber.type, fiber.memoizedProps))) | 453 | 451 | ) { | 454 | 452 | let nextInstance = nextHydratableInstance; | 455 | 453 | while (nextInstance) { | |
|