GeezerVison is showing github.com — Throw on hydration mismatch and force client rendering if boundary ha… · react/react@0ddd69d
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 +16-43Lines changed: 16 additions & 43 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1677,45 +1677,37 @@ describe('ReactDOMFizzServer', () => { | 1677 | 1677 |
| 1678 | 1678 | // @gate experimental | 1679 | 1679 | it('calls getServerSnapshot instead of getSnapshot', async () => { | 1680 | | - const ref = React.createRef(); | 1681 | | - | 1682 | 1680 | function getServerSnapshot() { | 1683 | 1681 | return 'server'; | 1684 | 1682 | } | 1685 | | - | 1686 | 1683 | function getClientSnapshot() { | 1687 | 1684 | return 'client'; | 1688 | 1685 | } | 1689 | | - | 1690 | 1686 | function subscribe() { | 1691 | 1687 | return () => {}; | 1692 | 1688 | } | 1693 | | - | 1694 | 1689 | function Child({text}) { | 1695 | 1690 | Scheduler.unstable_yieldValue(text); | 1696 | 1691 | return text; | 1697 | 1692 | } | 1698 | | - | 1699 | 1693 | function App() { | 1700 | 1694 | const value = useSyncExternalStore( | 1701 | 1695 | subscribe, | 1702 | 1696 | getClientSnapshot, | 1703 | 1697 | getServerSnapshot, | 1704 | 1698 | ); | 1705 | 1699 | return ( | 1706 | | - <div ref={ref}> | | 1700 | + <div> | 1707 | 1701 | <Child text={value} /> | 1708 | 1702 | </div> | 1709 | 1703 | ); | 1710 | 1704 | } | 1711 | | - | 1712 | 1705 | const loggedErrors = []; | 1713 | 1706 | await act(async () => { | 1714 | 1707 | const {pipe} = ReactDOMFizzServer.renderToPipeableStream( | 1715 | 1708 | <Suspense fallback="Loading..."> | 1716 | 1709 | <App /> | 1717 | 1710 | </Suspense>, | 1718 | | - | 1719 | 1711 | { | 1720 | 1712 | onError(x) { | 1721 | 1713 | loggedErrors.push(x); | @@ -1726,56 +1718,43 @@ describe('ReactDOMFizzServer', () => { | 1726 | 1718 | }); | 1727 | 1719 | expect(Scheduler).toHaveYielded(['server']); | 1728 | 1720 |
| 1729 | | - const serverRenderedDiv = container.getElementsByTagName('div')[0]; | 1730 | | - | 1731 | 1721 | ReactDOM.hydrateRoot(container, <App />); | 1732 | 1722 |
| 1733 | | - // The first paint uses the server snapshot | 1734 | | - expect(Scheduler).toFlushUntilNextPaint(['server']); | 1735 | | - expect(getVisibleChildren(container)).toEqual(<div>server</div>); | 1736 | | - // Hydration succeeded | 1737 | | - expect(ref.current).toEqual(serverRenderedDiv); | 1738 | | - | 1739 | | - // Asynchronously we detect that the store has changed on the client, | 1740 | | - // and patch up the inconsistency | 1741 | | - expect(Scheduler).toFlushUntilNextPaint(['client']); | | 1723 | + expect(() => { | | 1724 | + // The first paint switches to client rendering due to mismatch | | 1725 | + expect(Scheduler).toFlushUntilNextPaint(['client']); | | 1726 | + }).toErrorDev( | | 1727 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content', | | 1728 | + {withoutStack: true}, | | 1729 | + ); | 1742 | 1730 | expect(getVisibleChildren(container)).toEqual(<div>client</div>); | 1743 | | - expect(ref.current).toEqual(serverRenderedDiv); | 1744 | 1731 | }); | 1745 | 1732 |
| 1746 | 1733 | // The selector implementation uses the lazy ref initialization pattern | 1747 | | - // @gate !(enableUseRefAccessWarning && __DEV__) | 1748 | 1734 | // @gate experimental | 1749 | 1735 | it('calls getServerSnapshot instead of getSnapshot (with selector and isEqual)', async () => { | 1750 | 1736 | // Same as previous test, but with a selector that returns a complex object | 1751 | 1737 | // that is memoized with a custom `isEqual` function. | 1752 | 1738 | const ref = React.createRef(); | 1753 | | - | 1754 | 1739 | function getServerSnapshot() { | 1755 | 1740 | return {env: 'server', other: 'unrelated'}; | 1756 | 1741 | } | 1757 | | - | 1758 | 1742 | function getClientSnapshot() { | 1759 | 1743 | return {env: 'client', other: 'unrelated'}; | 1760 | 1744 | } | 1761 | | - | 1762 | 1745 | function selector({env}) { | 1763 | 1746 | return {env}; | 1764 | 1747 | } | 1765 | | - | 1766 | 1748 | function isEqual(a, b) { | 1767 | 1749 | return a.env === b.env; | 1768 | 1750 | } | 1769 | | - | 1770 | 1751 | function subscribe() { | 1771 | 1752 | return () => {}; | 1772 | 1753 | } | 1773 | | - | 1774 | 1754 | function Child({text}) { | 1775 | 1755 | Scheduler.unstable_yieldValue(text); | 1776 | 1756 | return text; | 1777 | 1757 | } | 1778 | | - | 1779 | 1758 | function App() { | 1780 | 1759 | const {env} = useSyncExternalStoreWithSelector( | 1781 | 1760 | subscribe, | @@ -1790,14 +1769,12 @@ describe('ReactDOMFizzServer', () => { | 1790 | 1769 | </div> | 1791 | 1770 | ); | 1792 | 1771 | } | 1793 | | - | 1794 | 1772 | const loggedErrors = []; | 1795 | 1773 | await act(async () => { | 1796 | 1774 | const {pipe} = ReactDOMFizzServer.renderToPipeableStream( | 1797 | 1775 | <Suspense fallback="Loading..."> | 1798 | 1776 | <App /> | 1799 | 1777 | </Suspense>, | 1800 | | - | 1801 | 1778 | { | 1802 | 1779 | onError(x) { | 1803 | 1780 | loggedErrors.push(x); | @@ -1808,21 +1785,17 @@ describe('ReactDOMFizzServer', () => { | 1808 | 1785 | }); | 1809 | 1786 | expect(Scheduler).toHaveYielded(['server']); | 1810 | 1787 |
| 1811 | | - const serverRenderedDiv = container.getElementsByTagName('div')[0]; | 1812 | | - | 1813 | 1788 | ReactDOM.hydrateRoot(container, <App />); | 1814 | 1789 |
| 1815 | | - // The first paint uses the server snapshot | 1816 | | - expect(Scheduler).toFlushUntilNextPaint(['server']); | 1817 | | - expect(getVisibleChildren(container)).toEqual(<div>server</div>); | 1818 | | - // Hydration succeeded | 1819 | | - expect(ref.current).toEqual(serverRenderedDiv); | 1820 | | - | 1821 | | - // Asynchronously we detect that the store has changed on the client, | 1822 | | - // and patch up the inconsistency | 1823 | | - expect(Scheduler).toFlushUntilNextPaint(['client']); | | 1790 | + // The first paint uses the client due to mismatch forcing client render | | 1791 | + expect(() => { | | 1792 | + // The first paint switches to client rendering due to mismatch | | 1793 | + expect(Scheduler).toFlushUntilNextPaint(['client']); | | 1794 | + }).toErrorDev( | | 1795 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content', | | 1796 | + {withoutStack: true}, | | 1797 | + ); | 1824 | 1798 | expect(getVisibleChildren(container)).toEqual(<div>client</div>); | 1825 | | - expect(ref.current).toEqual(serverRenderedDiv); | 1826 | 1799 | }); | 1827 | 1800 |
| 1828 | 1801 | // @gate experimental | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js +96-6Lines changed: 96 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -197,12 +197,7 @@ describe('ReactDOMServerPartialHydration', () => { | 197 | 197 | // hydrating anyway. | 198 | 198 | suspend = true; | 199 | 199 | ReactDOM.hydrateRoot(container, <App />); | 200 | | - expect(() => { | 201 | | - Scheduler.unstable_flushAll(); | 202 | | - }).toErrorDev( | 203 | | - // TODO: This error should not be logged in this case. It's a false positive. | 204 | | - 'Did not expect server HTML to contain the text node "Hello" in <div>.', | 205 | | - ); | | 200 | + Scheduler.unstable_flushAll(); | 206 | 201 | jest.runAllTimers(); | 207 | 202 |
| 208 | 203 | // Expect the server-generated HTML to stay intact. | @@ -218,6 +213,101 @@ describe('ReactDOMServerPartialHydration', () => { | 218 | 213 | expect(container.textContent).toBe('HelloHello'); | 219 | 214 | }); | 220 | 215 |
| | 216 | + it('falls back to client rendering boundary on mismatch', async () => { | | 217 | + let client = false; | | 218 | + let suspend = false; | | 219 | + let resolve; | | 220 | + const promise = new Promise(resolvePromise => { | | 221 | + resolve = () => { | | 222 | + suspend = false; | | 223 | + resolvePromise(); | | 224 | + }; | | 225 | + }); | | 226 | + function Child() { | | 227 | + if (suspend) { | | 228 | + Scheduler.unstable_yieldValue('Suspend'); | | 229 | + throw promise; | | 230 | + } else { | | 231 | + Scheduler.unstable_yieldValue('Hello'); | | 232 | + return 'Hello'; | | 233 | + } | | 234 | + } | | 235 | + function Component({shouldMismatch}) { | | 236 | + Scheduler.unstable_yieldValue('Component'); | | 237 | + if (shouldMismatch && client) { | | 238 | + return <article>Mismatch</article>; | | 239 | + } | | 240 | + return <div>Component</div>; | | 241 | + } | | 242 | + function App() { | | 243 | + return ( | | 244 | + <Suspense fallback="Loading..."> | | 245 | + <Child /> | | 246 | + <Component /> | | 247 | + <Component /> | | 248 | + <Component /> | | 249 | + <Component shouldMismatch={true} /> | | 250 | + </Suspense> | | 251 | + ); | | 252 | + } | | 253 | + const finalHTML = ReactDOMServer.renderToString(<App />); | | 254 | + const container = document.createElement('div'); | | 255 | + container.innerHTML = finalHTML; | | 256 | + expect(Scheduler).toHaveYielded([ | | 257 | + 'Hello', | | 258 | + 'Component', | | 259 | + 'Component', | | 260 | + 'Component', | | 261 | + 'Component', | | 262 | + ]); | | 263 | + | | 264 | + expect(container.innerHTML).toBe( | | 265 | + '<!--$-->Hello<div>Component</div><div>Component</div><div>Component</div><div>Component</div><!--/$-->', | | 266 | + ); | | 267 | + | | 268 | + suspend = true; | | 269 | + client = true; | | 270 | + | | 271 | + ReactDOM.hydrateRoot(container, <App />); | | 272 | + expect(Scheduler).toFlushAndYield([ | | 273 | + 'Suspend', | | 274 | + 'Component', | | 275 | + 'Component', | | 276 | + 'Component', | | 277 | + 'Component', | | 278 | + ]); | | 279 | + jest.runAllTimers(); | | 280 | + | | 281 | + // Unchanged | | 282 | + expect(container.innerHTML).toBe( | | 283 | + '<!--$-->Hello<div>Component</div><div>Component</div><div>Component</div><div>Component</div><!--/$-->', | | 284 | + ); | | 285 | + | | 286 | + suspend = false; | | 287 | + resolve(); | | 288 | + await promise; | | 289 | + | | 290 | + expect(Scheduler).toFlushAndYield([ | | 291 | + // first pass, mismatches at end | | 292 | + 'Hello', | | 293 | + 'Component', | | 294 | + 'Component', | | 295 | + 'Component', | | 296 | + 'Component', | | 297 | + // second pass as client render | | 298 | + 'Hello', | | 299 | + 'Component', | | 300 | + 'Component', | | 301 | + 'Component', | | 302 | + 'Component', | | 303 | + ]); | | 304 | + | | 305 | + // Client rendered - suspense comment nodes removed | | 306 | + expect(container.innerHTML).toBe( | | 307 | + 'Hello<div>Component</div><div>Component</div><div>Component</div><article>Mismatch</article>', | | 308 | + ); | | 309 | + }); | | 310 | + | 221 | 311 | it('calls the hydration callbacks after hydration or deletion', async () => { | 222 | 312 | let suspend = false; | 223 | 313 | let resolve; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.new.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | import type {Fiber} from './ReactInternalTypes'; | | 11 | +import {NoMode, ConcurrentMode} from './ReactTypeOfMode'; | 11 | 12 | import type { | 12 | 13 | Instance, | 13 | 14 | TextInstance, | @@ -323,12 +324,21 @@ function tryHydrate(fiber, nextInstance) { | 323 | 324 | } | 324 | 325 | } | 325 | 326 |
| | 327 | +function throwOnHydrationMismatchIfConcurrentMode(fiber) { | | 328 | + if ((fiber.mode & ConcurrentMode) !== NoMode) { | | 329 | + throw new Error( | | 330 | + 'An error occurred during hydration. The server HTML was replaced with client content', | | 331 | + ); | | 332 | + } | | 333 | +} | | 334 | + | 326 | 335 | function tryToClaimNextHydratableInstance(fiber: Fiber): void { | 327 | 336 | if (!isHydrating) { | 328 | 337 | return; | 329 | 338 | } | 330 | 339 | let nextInstance = nextHydratableInstance; | 331 | 340 | if (!nextInstance) { | | 341 | + throwOnHydrationMismatchIfConcurrentMode(fiber); | 332 | 342 | // Nothing to hydrate. Make it an insertion. | 333 | 343 | insertNonHydratedInstance((hydrationParentFiber: any), fiber); | 334 | 344 | isHydrating = false; | @@ -337,6 +347,7 @@ function tryToClaimNextHydratableInstance(fiber: Fiber): void { | 337 | 347 | } | 338 | 348 | const firstAttemptedInstance = nextInstance; | 339 | 349 | if (!tryHydrate(fiber, nextInstance)) { | | 350 | + throwOnHydrationMismatchIfConcurrentMode(fiber); | 340 | 351 | // If we can't hydrate this instance let's try the next one. | 341 | 352 | // We use this as a heuristic. It's based on intuition and not data so it | 342 | 353 | // might be flawed or unnecessary. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.old.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | import type {Fiber} from './ReactInternalTypes'; | | 11 | +import {NoMode, ConcurrentMode} from './ReactTypeOfMode'; | 11 | 12 | import type { | 12 | 13 | Instance, | 13 | 14 | TextInstance, | @@ -323,12 +324,21 @@ function tryHydrate(fiber, nextInstance) { | 323 | 324 | } | 324 | 325 | } | 325 | 326 |
| | 327 | +function throwOnHydrationMismatchIfConcurrentMode(fiber) { | | 328 | + if ((fiber.mode & ConcurrentMode) !== NoMode) { | | 329 | + throw new Error( | | 330 | + 'An error occurred during hydration. The server HTML was replaced with client content', | | 331 | + ); | | 332 | + } | | 333 | +} | | 334 | + | 326 | 335 | function tryToClaimNextHydratableInstance(fiber: Fiber): void { | 327 | 336 | if (!isHydrating) { | 328 | 337 | return; | 329 | 338 | } | 330 | 339 | let nextInstance = nextHydratableInstance; | 331 | 340 | if (!nextInstance) { | | 341 | + throwOnHydrationMismatchIfConcurrentMode(fiber); | 332 | 342 | // Nothing to hydrate. Make it an insertion. | 333 | 343 | insertNonHydratedInstance((hydrationParentFiber: any), fiber); | 334 | 344 | isHydrating = false; | @@ -337,6 +347,7 @@ function tryToClaimNextHydratableInstance(fiber: Fiber): void { | 337 | 347 | } | 338 | 348 | const firstAttemptedInstance = nextInstance; | 339 | 349 | if (!tryHydrate(fiber, nextInstance)) { | | 350 | + throwOnHydrationMismatchIfConcurrentMode(fiber); | 340 | 351 | // If we can't hydrate this instance let's try the next one. | 341 | 352 | // We use this as a heuristic. It's based on intuition and not data so it | 342 | 353 | // might be flawed or unnecessary. | |
|