GeezerVison is showing github.com — Refactor Partial Hydration (#16346) · react/react@50addf4
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__/ReactDOMServerPartialHydration-test.internal.js +75-2Lines changed: 75 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,77 @@ describe('ReactDOMServerPartialHydration', () => { | 90 | 90 | expect(ref.current).toBe(span); | 91 | 91 | }); | 92 | 92 |
| | 93 | + it('warns and replaces the boundary content in legacy mode', async () => { | | 94 | + let suspend = false; | | 95 | + let resolve; | | 96 | + let promise = new Promise(resolvePromise => (resolve = resolvePromise)); | | 97 | + let ref = React.createRef(); | | 98 | + | | 99 | + function Child() { | | 100 | + if (suspend) { | | 101 | + throw promise; | | 102 | + } else { | | 103 | + return 'Hello'; | | 104 | + } | | 105 | + } | | 106 | + | | 107 | + function App() { | | 108 | + return ( | | 109 | + <div> | | 110 | + <Suspense fallback="Loading..."> | | 111 | + <span ref={ref}> | | 112 | + <Child /> | | 113 | + </span> | | 114 | + </Suspense> | | 115 | + </div> | | 116 | + ); | | 117 | + } | | 118 | + | | 119 | + // Don't suspend on the server. | | 120 | + suspend = false; | | 121 | + let finalHTML = ReactDOMServer.renderToString(<App />); | | 122 | + | | 123 | + let container = document.createElement('div'); | | 124 | + container.innerHTML = finalHTML; | | 125 | + | | 126 | + let span = container.getElementsByTagName('span')[0]; | | 127 | + | | 128 | + // On the client we try to hydrate. | | 129 | + suspend = true; | | 130 | + expect(() => { | | 131 | + act(() => { | | 132 | + ReactDOM.hydrate(<App />, container); | | 133 | + }); | | 134 | + }).toWarnDev( | | 135 | + 'Warning: Cannot hydrate Suspense in legacy mode. Switch from ' + | | 136 | + 'ReactDOM.hydrate(element, container) to ' + | | 137 | + 'ReactDOM.unstable_createSyncRoot(container, { hydrate: true })' + | | 138 | + '.render(element) or remove the Suspense components from the server ' + | | 139 | + 'rendered components.' + | | 140 | + '\n in Suspense (at **)' + | | 141 | + '\n in div (at **)' + | | 142 | + '\n in App (at **)', | | 143 | + ); | | 144 | + | | 145 | + // We're now in loading state. | | 146 | + expect(container.textContent).toBe('Loading...'); | | 147 | + | | 148 | + let span2 = container.getElementsByTagName('span')[0]; | | 149 | + // This is a new node. | | 150 | + expect(span).not.toBe(span2); | | 151 | + expect(ref.current).toBe(span2); | | 152 | + | | 153 | + // Resolving the promise should render the final content. | | 154 | + suspend = false; | | 155 | + resolve(); | | 156 | + await promise; | | 157 | + Scheduler.unstable_flushAll(); | | 158 | + jest.runAllTimers(); | | 159 | + | | 160 | + // We should now have hydrated with a ref on the existing span. | | 161 | + expect(container.textContent).toBe('Hello'); | | 162 | + }); | | 163 | + | 93 | 164 | it('can insert siblings before the dehydrated boundary', () => { | 94 | 165 | let suspend = false; | 95 | 166 | let promise = new Promise(() => {}); | @@ -135,7 +206,8 @@ describe('ReactDOMServerPartialHydration', () => { | 135 | 206 | suspend = true; | 136 | 207 |
| 137 | 208 | act(() => { | 138 | | - ReactDOM.hydrate(<App />, container); | | 209 | + let root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | | 210 | + root.render(<App />); | 139 | 211 | }); | 140 | 212 |
| 141 | 213 | expect(container.firstChild.firstChild.tagName).not.toBe('DIV'); | @@ -191,7 +263,8 @@ describe('ReactDOMServerPartialHydration', () => { | 191 | 263 | // hydrating anyway. | 192 | 264 | suspend = true; | 193 | 265 | act(() => { | 194 | | - ReactDOM.hydrate(<App />, container); | | 266 | + let root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | | 267 | + root.render(<App />); | 195 | 268 | }); | 196 | 269 |
| 197 | 270 | expect(container.firstChild.children[1].textContent).toBe('Middle'); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerSuspense-test.internal.js +16-4Lines changed: 16 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,7 @@ function initModules() { | 37 | 37 | }; | 38 | 38 | } | 39 | 39 |
| 40 | | -const {resetModules, serverRender, itRenders} = ReactDOMServerIntegrationUtils( | | 40 | +const {resetModules, serverRender} = ReactDOMServerIntegrationUtils( | 41 | 41 | initModules, | 42 | 42 | ); | 43 | 43 |
| @@ -102,23 +102,35 @@ describe('ReactDOMServerSuspense', () => { | 102 | 102 | ); | 103 | 103 | }); | 104 | 104 |
| 105 | | - itRenders('a SuspenseList component and its children', async render => { | 106 | | - const element = await render( | | 105 | + it('server renders a SuspenseList component and its children', async () => { | | 106 | + const example = ( | 107 | 107 | <React.unstable_SuspenseList> | 108 | 108 | <React.Suspense fallback="Loading A"> | 109 | 109 | <div>A</div> | 110 | 110 | </React.Suspense> | 111 | 111 | <React.Suspense fallback="Loading B"> | 112 | 112 | <div>B</div> | 113 | 113 | </React.Suspense> | 114 | | - </React.unstable_SuspenseList>, | | 114 | + </React.unstable_SuspenseList> | 115 | 115 | ); | | 116 | + const element = await serverRender(example); | 116 | 117 | const parent = element.parentNode; | 117 | 118 | const divA = parent.children[0]; | 118 | 119 | expect(divA.tagName).toBe('DIV'); | 119 | 120 | expect(divA.textContent).toBe('A'); | 120 | 121 | const divB = parent.children[1]; | 121 | 122 | expect(divB.tagName).toBe('DIV'); | 122 | 123 | expect(divB.textContent).toBe('B'); | | 124 | + | | 125 | + ReactTestUtils.act(() => { | | 126 | + const root = ReactDOM.unstable_createSyncRoot(parent, {hydrate: true}); | | 127 | + root.render(example); | | 128 | + }); | | 129 | + | | 130 | + const parent2 = element.parentNode; | | 131 | + const divA2 = parent2.children[0]; | | 132 | + const divB2 = parent2.children[1]; | | 133 | + expect(divA).toBe(divA2); | | 134 | + expect(divB).toBe(divB2); | 123 | 135 | }); | 124 | 136 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactDebugFiberPerf.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,6 @@ import { | 21 | 21 | ContextConsumer, | 22 | 22 | Mode, | 23 | 23 | SuspenseComponent, | 24 | | - DehydratedSuspenseComponent, | 25 | 24 | } from 'shared/ReactWorkTags'; | 26 | 25 |
| 27 | 26 | type MeasurementPhase = | @@ -317,8 +316,7 @@ export function stopFailedWorkTimer(fiber: Fiber): void { | 317 | 316 | } | 318 | 317 | fiber._debugIsCurrentlyTiming = false; | 319 | 318 | const warning = | 320 | | - fiber.tag === SuspenseComponent || | 321 | | - fiber.tag === DehydratedSuspenseComponent | | 319 | + fiber.tag === SuspenseComponent | 322 | 320 | ? 'Rendering was suspended' | 323 | 321 | : 'An error was thrown inside this error boundary'; | 324 | 322 | endFiberMark(fiber, null, warning); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ import type {ExpirationTime} from './ReactFiberExpirationTime'; | 24 | 24 | import type {UpdateQueue} from './ReactUpdateQueue'; | 25 | 25 | import type {ContextDependency} from './ReactFiberNewContext'; | 26 | 26 | import type {HookType} from './ReactFiberHooks'; | | 27 | +import type {SuspenseInstance} from './ReactFiberHostConfig'; | 27 | 28 |
| 28 | 29 | import invariant from 'shared/invariant'; | 29 | 30 | import warningWithoutStack from 'shared/warningWithoutStack'; | @@ -48,6 +49,7 @@ import { | 48 | 49 | Profiler, | 49 | 50 | SuspenseComponent, | 50 | 51 | SuspenseListComponent, | | 52 | + DehydratedFragment, | 51 | 53 | FunctionComponent, | 52 | 54 | MemoComponent, | 53 | 55 | SimpleMemoComponent, | @@ -843,6 +845,14 @@ export function createFiberFromHostInstanceForDeletion(): Fiber { | 843 | 845 | return fiber; | 844 | 846 | } | 845 | 847 |
| | 848 | +export function createFiberFromDehydratedFragment( | | 849 | + dehydratedNode: SuspenseInstance, | | 850 | +): Fiber { | | 851 | + const fiber = createFiber(DehydratedFragment, null, null, NoMode); | | 852 | + fiber.stateNode = dehydratedNode; | | 853 | + return fiber; | | 854 | +} | | 855 | + | 846 | 856 | export function createFiberFromPortal( | 847 | 857 | portal: ReactPortal, | 848 | 858 | mode: TypeOfMode, | |
|