GeezerVison is showing github.com — Hydrate using SuspenseComponent as the parent (#22582) · react/react@2af4a79
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 +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -160,6 +160,64 @@ describe('ReactDOMServerPartialHydration', () => { | 160 | 160 | expect(ref.current).toBe(span); | 161 | 161 | }); | 162 | 162 |
| | 163 | + it('can hydrate siblings of a suspended component without errors', async () => { | | 164 | + let suspend = false; | | 165 | + let resolve; | | 166 | + const promise = new Promise(resolvePromise => (resolve = resolvePromise)); | | 167 | + function Child() { | | 168 | + if (suspend) { | | 169 | + throw promise; | | 170 | + } else { | | 171 | + return 'Hello'; | | 172 | + } | | 173 | + } | | 174 | + | | 175 | + function App() { | | 176 | + return ( | | 177 | + <Suspense fallback="Loading..."> | | 178 | + <Child /> | | 179 | + <Suspense fallback="Loading..."> | | 180 | + <div>Hello</div> | | 181 | + </Suspense> | | 182 | + </Suspense> | | 183 | + ); | | 184 | + } | | 185 | + | | 186 | + // First we render the final HTML. With the streaming renderer | | 187 | + // this may have suspense points on the server but here we want | | 188 | + // to test the completed HTML. Don't suspend on the server. | | 189 | + suspend = false; | | 190 | + const finalHTML = ReactDOMServer.renderToString(<App />); | | 191 | + | | 192 | + const container = document.createElement('div'); | | 193 | + container.innerHTML = finalHTML; | | 194 | + expect(container.textContent).toBe('HelloHello'); | | 195 | + | | 196 | + // On the client we don't have all data yet but we want to start | | 197 | + // hydrating anyway. | | 198 | + suspend = true; | | 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 | + ); | | 206 | + jest.runAllTimers(); | | 207 | + | | 208 | + // Expect the server-generated HTML to stay intact. | | 209 | + expect(container.textContent).toBe('HelloHello'); | | 210 | + | | 211 | + // Resolving the promise should continue hydration | | 212 | + suspend = false; | | 213 | + resolve(); | | 214 | + await promise; | | 215 | + Scheduler.unstable_flushAll(); | | 216 | + jest.runAllTimers(); | | 217 | + // Hydration should not change anything. | | 218 | + expect(container.textContent).toBe('HelloHello'); | | 219 | + }); | | 220 | + | 163 | 221 | it('calls the hydration callbacks after hydration or deletion', async () => { | 164 | 222 | let suspend = false; | 165 | 223 | let resolve; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMHostConfig.js +69-5Lines changed: 69 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -764,11 +764,23 @@ export function getNextHydratableSibling( | 764 | 764 | } | 765 | 765 |
| 766 | 766 | export function getFirstHydratableChild( | 767 | | - parentInstance: Container | Instance, | | 767 | + parentInstance: Instance, | 768 | 768 | ): null | HydratableInstance { | 769 | 769 | return getNextHydratable(parentInstance.firstChild); | 770 | 770 | } | 771 | 771 |
| | 772 | +export function getFirstHydratableChildWithinContainer( | | 773 | + parentContainer: Container, | | 774 | +): null | HydratableInstance { | | 775 | + return getNextHydratable(parentContainer.firstChild); | | 776 | +} | | 777 | + | | 778 | +export function getFirstHydratableChildWithinSuspenseInstance( | | 779 | + parentInstance: SuspenseInstance, | | 780 | +): null | HydratableInstance { | | 781 | + return getNextHydratable(parentInstance.nextSibling); | | 782 | +} | | 783 | + | 772 | 784 | export function hydrateInstance( | 773 | 785 | instance: Instance, | 774 | 786 | type: string, | @@ -917,7 +929,7 @@ export function didNotMatchHydratedTextInstance( | 917 | 929 | } | 918 | 930 | } | 919 | 931 |
| 920 | | -export function didNotHydrateContainerInstance( | | 932 | +export function didNotHydrateInstanceWithinContainer( | 921 | 933 | parentContainer: Container, | 922 | 934 | instance: HydratableInstance, | 923 | 935 | ) { | @@ -932,6 +944,25 @@ export function didNotHydrateContainerInstance( | 932 | 944 | } | 933 | 945 | } | 934 | 946 |
| | 947 | +export function didNotHydrateInstanceWithinSuspenseInstance( | | 948 | + parentInstance: SuspenseInstance, | | 949 | + instance: HydratableInstance, | | 950 | +) { | | 951 | + if (__DEV__) { | | 952 | + // $FlowFixMe: Only Element or Document can be parent nodes. | | 953 | + const parentNode: Element | Document | null = parentInstance.parentNode; | | 954 | + if (parentNode !== null) { | | 955 | + if (instance.nodeType === ELEMENT_NODE) { | | 956 | + warnForDeletedHydratableElement(parentNode, (instance: any)); | | 957 | + } else if (instance.nodeType === COMMENT_NODE) { | | 958 | + // TODO: warnForDeletedHydratableSuspenseBoundary | | 959 | + } else { | | 960 | + warnForDeletedHydratableText(parentNode, (instance: any)); | | 961 | + } | | 962 | + } | | 963 | + } | | 964 | +} | | 965 | + | 935 | 966 | export function didNotHydrateInstance( | 936 | 967 | parentType: string, | 937 | 968 | parentProps: Props, | @@ -949,7 +980,7 @@ export function didNotHydrateInstance( | 949 | 980 | } | 950 | 981 | } | 951 | 982 |
| 952 | | -export function didNotFindHydratableContainerInstance( | | 983 | +export function didNotFindHydratableInstanceWithinContainer( | 953 | 984 | parentContainer: Container, | 954 | 985 | type: string, | 955 | 986 | props: Props, | @@ -959,7 +990,7 @@ export function didNotFindHydratableContainerInstance( | 959 | 990 | } | 960 | 991 | } | 961 | 992 |
| 962 | | -export function didNotFindHydratableContainerTextInstance( | | 993 | +export function didNotFindHydratableTextInstanceWithinContainer( | 963 | 994 | parentContainer: Container, | 964 | 995 | text: string, | 965 | 996 | ) { | @@ -968,14 +999,47 @@ export function didNotFindHydratableContainerTextInstance( | 968 | 999 | } | 969 | 1000 | } | 970 | 1001 |
| 971 | | -export function didNotFindHydratableContainerSuspenseInstance( | | 1002 | +export function didNotFindHydratableSuspenseInstanceWithinContainer( | 972 | 1003 | parentContainer: Container, | 973 | 1004 | ) { | 974 | 1005 | if (__DEV__) { | 975 | 1006 | // TODO: warnForInsertedHydratedSuspense(parentContainer); | 976 | 1007 | } | 977 | 1008 | } | 978 | 1009 |
| | 1010 | +export function didNotFindHydratableInstanceWithinSuspenseInstance( | | 1011 | + parentInstance: SuspenseInstance, | | 1012 | + type: string, | | 1013 | + props: Props, | | 1014 | +) { | | 1015 | + if (__DEV__) { | | 1016 | + // $FlowFixMe: Only Element or Document can be parent nodes. | | 1017 | + const parentNode: Element | Document | null = parentInstance.parentNode; | | 1018 | + if (parentNode !== null) | | 1019 | + warnForInsertedHydratedElement(parentNode, type, props); | | 1020 | + } | | 1021 | +} | | 1022 | + | | 1023 | +export function didNotFindHydratableTextInstanceWithinSuspenseInstance( | | 1024 | + parentInstance: SuspenseInstance, | | 1025 | + text: string, | | 1026 | +) { | | 1027 | + if (__DEV__) { | | 1028 | + // $FlowFixMe: Only Element or Document can be parent nodes. | | 1029 | + const parentNode: Element | Document | null = parentInstance.parentNode; | | 1030 | + if (parentNode !== null) warnForInsertedHydratedText(parentNode, text); | | 1031 | + } | | 1032 | +} | | 1033 | + | | 1034 | +export function didNotFindHydratableSuspenseInstanceWithinSuspenseInstance( | | 1035 | + parentInstance: SuspenseInstance, | | 1036 | +) { | | 1037 | + if (__DEV__) { | | 1038 | + // const parentNode: Element | Document | null = parentInstance.parentNode; | | 1039 | + // TODO: warnForInsertedHydratedSuspense(parentNode); | | 1040 | + } | | 1041 | +} | | 1042 | + | 979 | 1043 | export function didNotFindHydratableInstance( | 980 | 1044 | parentType: string, | 981 | 1045 | parentProps: Props, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHostConfigWithNoHydration.js +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,8 @@ export const isSuspenseInstanceFallback = shim; | 29 | 29 | export const registerSuspenseInstanceRetry = shim; | 30 | 30 | export const getNextHydratableSibling = shim; | 31 | 31 | export const getFirstHydratableChild = shim; | | 32 | +export const getFirstHydratableChildWithinContainer = shim; | | 33 | +export const getFirstHydratableChildWithinSuspenseInstance = shim; | 32 | 34 | export const hydrateInstance = shim; | 33 | 35 | export const hydrateTextInstance = shim; | 34 | 36 | export const hydrateSuspenseInstance = shim; | @@ -40,11 +42,15 @@ export const clearSuspenseBoundaryFromContainer = shim; | 40 | 42 | export const shouldDeleteUnhydratedTailInstances = shim; | 41 | 43 | export const didNotMatchHydratedContainerTextInstance = shim; | 42 | 44 | export const didNotMatchHydratedTextInstance = shim; | 43 | | -export const didNotHydrateContainerInstance = shim; | | 45 | +export const didNotHydrateInstanceWithinContainer = shim; | | 46 | +export const didNotHydrateInstanceWithinSuspenseInstance = shim; | 44 | 47 | export const didNotHydrateInstance = shim; | 45 | | -export const didNotFindHydratableContainerInstance = shim; | 46 | | -export const didNotFindHydratableContainerTextInstance = shim; | 47 | | -export const didNotFindHydratableContainerSuspenseInstance = shim; | | 48 | +export const didNotFindHydratableInstanceWithinContainer = shim; | | 49 | +export const didNotFindHydratableTextInstanceWithinContainer = shim; | | 50 | +export const didNotFindHydratableSuspenseInstanceWithinContainer = shim; | | 51 | +export const didNotFindHydratableInstanceWithinSuspenseInstance = shim; | | 52 | +export const didNotFindHydratableTextInstanceWithinSuspenseInstance = shim; | | 53 | +export const didNotFindHydratableSuspenseInstanceWithinSuspenseInstance = shim; | 48 | 54 | export const didNotFindHydratableInstance = shim; | 49 | 55 | export const didNotFindHydratableTextInstance = shim; | 50 | 56 | export const didNotFindHydratableSuspenseInstance = shim; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHydrationContext.new.js +68-12Lines changed: 68 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,18 +38,24 @@ import { | 38 | 38 | canHydrateSuspenseInstance, | 39 | 39 | getNextHydratableSibling, | 40 | 40 | getFirstHydratableChild, | | 41 | + getFirstHydratableChildWithinContainer, | | 42 | + getFirstHydratableChildWithinSuspenseInstance, | 41 | 43 | hydrateInstance, | 42 | 44 | hydrateTextInstance, | 43 | 45 | hydrateSuspenseInstance, | 44 | 46 | getNextHydratableInstanceAfterSuspenseInstance, | 45 | 47 | shouldDeleteUnhydratedTailInstances, | 46 | 48 | didNotMatchHydratedContainerTextInstance, | 47 | 49 | didNotMatchHydratedTextInstance, | 48 | | - didNotHydrateContainerInstance, | | 50 | + didNotHydrateInstanceWithinContainer, | | 51 | + didNotHydrateInstanceWithinSuspenseInstance, | 49 | 52 | didNotHydrateInstance, | 50 | | - didNotFindHydratableContainerInstance, | 51 | | - didNotFindHydratableContainerTextInstance, | 52 | | - didNotFindHydratableContainerSuspenseInstance, | | 53 | + didNotFindHydratableInstanceWithinContainer, | | 54 | + didNotFindHydratableTextInstanceWithinContainer, | | 55 | + didNotFindHydratableSuspenseInstanceWithinContainer, | | 56 | + didNotFindHydratableInstanceWithinSuspenseInstance, | | 57 | + didNotFindHydratableTextInstanceWithinSuspenseInstance, | | 58 | + didNotFindHydratableSuspenseInstanceWithinSuspenseInstance, | 53 | 59 | didNotFindHydratableInstance, | 54 | 60 | didNotFindHydratableTextInstance, | 55 | 61 | didNotFindHydratableSuspenseInstance, | @@ -78,8 +84,10 @@ function enterHydrationState(fiber: Fiber): boolean { | 78 | 84 | return false; | 79 | 85 | } | 80 | 86 |
| 81 | | - const parentInstance = fiber.stateNode.containerInfo; | 82 | | - nextHydratableInstance = getFirstHydratableChild(parentInstance); | | 87 | + const parentInstance: Container = fiber.stateNode.containerInfo; | | 88 | + nextHydratableInstance = getFirstHydratableChildWithinContainer( | | 89 | + parentInstance, | | 90 | + ); | 83 | 91 | hydrationParentFiber = fiber; | 84 | 92 | isHydrating = true; | 85 | 93 | return true; | @@ -92,8 +100,10 @@ function reenterHydrationStateFromDehydratedSuspenseInstance( | 92 | 100 | if (!supportsHydration) { | 93 | 101 | return false; | 94 | 102 | } | 95 | | - nextHydratableInstance = getNextHydratableSibling(suspenseInstance); | 96 | | - popToNextHostParent(fiber); | | 103 | + nextHydratableInstance = getFirstHydratableChildWithinSuspenseInstance( | | 104 | + suspenseInstance, | | 105 | + ); | | 106 | + hydrationParentFiber = fiber; | 97 | 107 | isHydrating = true; | 98 | 108 | return true; | 99 | 109 | } | @@ -105,7 +115,7 @@ function deleteHydratableInstance( | 105 | 115 | if (__DEV__) { | 106 | 116 | switch (returnFiber.tag) { | 107 | 117 | case HostRoot: | 108 | | - didNotHydrateContainerInstance( | | 118 | + didNotHydrateInstanceWithinContainer( | 109 | 119 | returnFiber.stateNode.containerInfo, | 110 | 120 | instance, | 111 | 121 | ); | @@ -118,6 +128,14 @@ function deleteHydratableInstance( | 118 | 128 | instance, | 119 | 129 | ); | 120 | 130 | break; | | 131 | + case SuspenseComponent: | | 132 | + const suspenseState: SuspenseState = returnFiber.memoizedState; | | 133 | + if (suspenseState.dehydrated !== null) | | 134 | + didNotHydrateInstanceWithinSuspenseInstance( | | 135 | + suspenseState.dehydrated, | | 136 | + instance, | | 137 | + ); | | 138 | + break; | 121 | 139 | } | 122 | 140 | } | 123 | 141 |
| @@ -144,14 +162,23 @@ function insertNonHydratedInstance(returnFiber: Fiber, fiber: Fiber) { | 144 | 162 | case HostComponent: | 145 | 163 | const type = fiber.type; | 146 | 164 | const props = fiber.pendingProps; | 147 | | - didNotFindHydratableContainerInstance(parentContainer, type, props); | | 165 | + didNotFindHydratableInstanceWithinContainer( | | 166 | + parentContainer, | | 167 | + type, | | 168 | + props, | | 169 | + ); | 148 | 170 | break; | 149 | 171 | case HostText: | 150 | 172 | const text = fiber.pendingProps; | 151 | | - didNotFindHydratableContainerTextInstance(parentContainer, text); | | 173 | + didNotFindHydratableTextInstanceWithinContainer( | | 174 | + parentContainer, | | 175 | + text, | | 176 | + ); | 152 | 177 | break; | 153 | 178 | case SuspenseComponent: | 154 | | - didNotFindHydratableContainerSuspenseInstance(parentContainer); | | 179 | + didNotFindHydratableSuspenseInstanceWithinContainer( | | 180 | + parentContainer, | | 181 | + ); | 155 | 182 | break; | 156 | 183 | } | 157 | 184 | break; | @@ -191,6 +218,35 @@ function insertNonHydratedInstance(returnFiber: Fiber, fiber: Fiber) { | 191 | 218 | } | 192 | 219 | break; | 193 | 220 | } | | 221 | + case SuspenseComponent: { | | 222 | + const suspenseState: SuspenseState = returnFiber.memoizedState; | | 223 | + const parentInstance = suspenseState.dehydrated; | | 224 | + if (parentInstance !== null) | | 225 | + switch (fiber.tag) { | | 226 | + case HostComponent: | | 227 | + const type = fiber.type; | | 228 | + const props = fiber.pendingProps; | | 229 | + didNotFindHydratableInstanceWithinSuspenseInstance( | | 230 | + parentInstance, | | 231 | + type, | | 232 | + props, | | 233 | + ); | | 234 | + break; | | 235 | + case HostText: | | 236 | + const text = fiber.pendingProps; | | 237 | + didNotFindHydratableTextInstanceWithinSuspenseInstance( | | 238 | + parentInstance, | | 239 | + text, | | 240 | + ); | | 241 | + break; | | 242 | + case SuspenseComponent: | | 243 | + didNotFindHydratableSuspenseInstanceWithinSuspenseInstance( | | 244 | + parentInstance, | | 245 | + ); | | 246 | + break; | | 247 | + } | | 248 | + break; | | 249 | + } | 194 | 250 | default: | 195 | 251 | return; | 196 | 252 | } | |
|