GeezerVison is showing github.com — Track Owner for Server Components in DEV (#28753) · hanzoai/react@67fb9a6
File treeExpand file treeCollapse file tree Open diff view settings react-devtools-shared/src react-native-renderer/src react-server-dom-webpack/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +18-7Lines changed: 18 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -484,6 +484,7 @@ function createElement( | 484 | 484 | type: mixed, | 485 | 485 | key: mixed, | 486 | 486 | props: mixed, | | 487 | + owner: null | ReactComponentInfo, // DEV-only | 487 | 488 | ): React$Element<any> { | 488 | 489 | let element: any; | 489 | 490 | if (__DEV__ && enableRefAsProp) { | @@ -493,7 +494,7 @@ function createElement( | 493 | 494 | type, | 494 | 495 | key, | 495 | 496 | props, | 496 | | - _owner: null, | | 497 | + _owner: owner, | 497 | 498 | }: any); | 498 | 499 | Object.defineProperty(element, 'ref', { | 499 | 500 | enumerable: false, | @@ -520,7 +521,7 @@ function createElement( | 520 | 521 | props, | 521 | 522 |
| 522 | 523 | // Record the component responsible for creating this element. | 523 | | - _owner: null, | | 524 | + _owner: owner, | 524 | 525 | }: any); | 525 | 526 | } | 526 | 527 |
| @@ -854,7 +855,12 @@ function parseModelTuple( | 854 | 855 | if (tuple[0] === REACT_ELEMENT_TYPE) { | 855 | 856 | // TODO: Consider having React just directly accept these arrays as elements. | 856 | 857 | // Or even change the ReactElement type to be an array. | 857 | | - return createElement(tuple[1], tuple[2], tuple[3]); | | 858 | + return createElement( | | 859 | + tuple[1], | | 860 | + tuple[2], | | 861 | + tuple[3], | | 862 | + __DEV__ ? (tuple: any)[4] : null, | | 863 | + ); | 858 | 864 | } | 859 | 865 | return value; | 860 | 866 | } | @@ -1132,12 +1138,14 @@ function resolveConsoleEntry( | 1132 | 1138 | ); | 1133 | 1139 | } | 1134 | 1140 |
| 1135 | | - const payload: [string, string, string, mixed] = parseModel(response, value); | | 1141 | + const payload: [string, string, null | ReactComponentInfo, string, mixed] = | | 1142 | + parseModel(response, value); | 1136 | 1143 | const methodName = payload[0]; | 1137 | 1144 | // TODO: Restore the fake stack before logging. | 1138 | 1145 | // const stackTrace = payload[1]; | 1139 | | - const env = payload[2]; | 1140 | | - const args = payload.slice(3); | | 1146 | + // const owner = payload[2]; | | 1147 | + const env = payload[3]; | | 1148 | + const args = payload.slice(4); | 1141 | 1149 | printToConsole(methodName, args, env); | 1142 | 1150 | } | 1143 | 1151 |
| @@ -1286,7 +1294,10 @@ function processFullRow( | 1286 | 1294 | } | 1287 | 1295 | case 68 /* "D" */: { | 1288 | 1296 | if (__DEV__) { | 1289 | | - const debugInfo = JSON.parse(row); | | 1297 | + const debugInfo: ReactComponentInfo | ReactAsyncInfo = parseModel( | | 1298 | + response, | | 1299 | + row, | | 1300 | + ); | 1290 | 1301 | resolveDebugInfo(response, id, debugInfo); | 1291 | 1302 | return; | 1292 | 1303 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/__tests__/ReactFlight-test.js +53-5Lines changed: 53 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -214,7 +214,7 @@ describe('ReactFlight', () => { | 214 | 214 | const rootModel = await ReactNoopFlightClient.read(transport); | 215 | 215 | const greeting = rootModel.greeting; | 216 | 216 | expect(greeting._debugInfo).toEqual( | 217 | | - __DEV__ ? [{name: 'Greeting', env: 'Server'}] : undefined, | | 217 | + __DEV__ ? [{name: 'Greeting', env: 'Server', owner: null}] : undefined, | 218 | 218 | ); | 219 | 219 | ReactNoop.render(greeting); | 220 | 220 | }); | @@ -241,7 +241,7 @@ describe('ReactFlight', () => { | 241 | 241 | await act(async () => { | 242 | 242 | const promise = ReactNoopFlightClient.read(transport); | 243 | 243 | expect(promise._debugInfo).toEqual( | 244 | | - __DEV__ ? [{name: 'Greeting', env: 'Server'}] : undefined, | | 244 | + __DEV__ ? [{name: 'Greeting', env: 'Server', owner: null}] : undefined, | 245 | 245 | ); | 246 | 246 | ReactNoop.render(await promise); | 247 | 247 | }); | @@ -2072,19 +2072,21 @@ describe('ReactFlight', () => { | 2072 | 2072 | await act(async () => { | 2073 | 2073 | const promise = ReactNoopFlightClient.read(transport); | 2074 | 2074 | expect(promise._debugInfo).toEqual( | 2075 | | - __DEV__ ? [{name: 'ServerComponent', env: 'Server'}] : undefined, | | 2075 | + __DEV__ | | 2076 | + ? [{name: 'ServerComponent', env: 'Server', owner: null}] | | 2077 | + : undefined, | 2076 | 2078 | ); | 2077 | 2079 | const result = await promise; | 2078 | 2080 | const thirdPartyChildren = await result.props.children[1]; | 2079 | 2081 | // We expect the debug info to be transferred from the inner stream to the outer. | 2080 | 2082 | expect(thirdPartyChildren[0]._debugInfo).toEqual( | 2081 | 2083 | __DEV__ | 2082 | | - ? [{name: 'ThirdPartyComponent', env: 'third-party'}] | | 2084 | + ? [{name: 'ThirdPartyComponent', env: 'third-party', owner: null}] | 2083 | 2085 | : undefined, | 2084 | 2086 | ); | 2085 | 2087 | expect(thirdPartyChildren[1]._debugInfo).toEqual( | 2086 | 2088 | __DEV__ | 2087 | | - ? [{name: 'ThirdPartyLazyComponent', env: 'third-party'}] | | 2089 | + ? [{name: 'ThirdPartyLazyComponent', env: 'third-party', owner: null}] | 2088 | 2090 | : undefined, | 2089 | 2091 | ); | 2090 | 2092 | ReactNoop.render(result); | @@ -2145,4 +2147,50 @@ describe('ReactFlight', () => { | 2145 | 2147 | expect(loggedFn).not.toBe(foo); | 2146 | 2148 | expect(loggedFn.toString()).toBe(foo.toString()); | 2147 | 2149 | }); | | 2150 | + | | 2151 | + it('uses the server component debug info as the element owner in DEV', async () => { | | 2152 | + function Container({children}) { | | 2153 | + return children; | | 2154 | + } | | 2155 | + | | 2156 | + function Greeting({firstName}) { | | 2157 | + // We can't use JSX here because it'll use the Client React. | | 2158 | + return ReactServer.createElement( | | 2159 | + Container, | | 2160 | + null, | | 2161 | + ReactServer.createElement('span', null, 'Hello, ', firstName), | | 2162 | + ); | | 2163 | + } | | 2164 | + | | 2165 | + const model = { | | 2166 | + greeting: ReactServer.createElement(Greeting, {firstName: 'Seb'}), | | 2167 | + }; | | 2168 | + | | 2169 | + const transport = ReactNoopFlightServer.render(model); | | 2170 | + | | 2171 | + await act(async () => { | | 2172 | + const rootModel = await ReactNoopFlightClient.read(transport); | | 2173 | + const greeting = rootModel.greeting; | | 2174 | + // We've rendered down to the span. | | 2175 | + expect(greeting.type).toBe('span'); | | 2176 | + if (__DEV__) { | | 2177 | + const greetInfo = {name: 'Greeting', env: 'Server', owner: null}; | | 2178 | + expect(greeting._debugInfo).toEqual([ | | 2179 | + greetInfo, | | 2180 | + {name: 'Container', env: 'Server', owner: greetInfo}, | | 2181 | + ]); | | 2182 | + // The owner that created the span was the outer server component. | | 2183 | + // We expect the debug info to be referentially equal to the owner. | | 2184 | + expect(greeting._owner).toBe(greeting._debugInfo[0]); | | 2185 | + } else { | | 2186 | + expect(greeting._debugInfo).toBe(undefined); | | 2187 | + expect(greeting._owner).toBe( | | 2188 | + gate(flags => flags.disableStringRefs) ? undefined : null, | | 2189 | + ); | | 2190 | + } | | 2191 | + ReactNoop.render(greeting); | | 2192 | + }); | | 2193 | + | | 2194 | + expect(ReactNoop).toMatchRenderedOutput(<span>Hello, Seb</span>); | | 2195 | + }); | 2148 | 2196 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/__tests__/componentStacks-test.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,6 +101,7 @@ describe('component stack', () => { | 101 | 101 | { | 102 | 102 | name: 'ServerComponent', | 103 | 103 | env: 'Server', | | 104 | + owner: null, | 104 | 105 | }, | 105 | 106 | ]; | 106 | 107 | const Parent = () => ChildPromise; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/DevToolsComponentStackFrame.js +5-17Lines changed: 5 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,10 +33,7 @@ import { | 33 | 33 | import {disableLogs, reenableLogs} from './DevToolsConsolePatching'; | 34 | 34 |
| 35 | 35 | let prefix; | 36 | | -export function describeBuiltInComponentFrame( | 37 | | - name: string, | 38 | | - ownerFn: void | null | Function, | 39 | | -): string { | | 36 | +export function describeBuiltInComponentFrame(name: string): string { | 40 | 37 | if (prefix === undefined) { | 41 | 38 | // Extract the VM specific prefix used by each line. | 42 | 39 | try { | @@ -51,10 +48,7 @@ export function describeBuiltInComponentFrame( | 51 | 48 | } | 52 | 49 |
| 53 | 50 | export function describeDebugInfoFrame(name: string, env: ?string): string { | 54 | | - return describeBuiltInComponentFrame( | 55 | | - name + (env ? ' (' + env + ')' : ''), | 56 | | - null, | 57 | | - ); | | 51 | + return describeBuiltInComponentFrame(name + (env ? ' (' + env + ')' : '')); | 58 | 52 | } | 59 | 53 |
| 60 | 54 | let reentry = false; | @@ -292,15 +286,13 @@ export function describeNativeComponentFrame( | 292 | 286 |
| 293 | 287 | export function describeClassComponentFrame( | 294 | 288 | ctor: Function, | 295 | | - ownerFn: void | null | Function, | 296 | 289 | currentDispatcherRef: CurrentDispatcherRef, | 297 | 290 | ): string { | 298 | 291 | return describeNativeComponentFrame(ctor, true, currentDispatcherRef); | 299 | 292 | } | 300 | 293 |
| 301 | 294 | export function describeFunctionComponentFrame( | 302 | 295 | fn: Function, | 303 | | - ownerFn: void | null | Function, | 304 | 296 | currentDispatcherRef: CurrentDispatcherRef, | 305 | 297 | ): string { | 306 | 298 | return describeNativeComponentFrame(fn, false, currentDispatcherRef); | @@ -313,7 +305,6 @@ function shouldConstruct(Component: Function) { | 313 | 305 |
| 314 | 306 | export function describeUnknownElementTypeFrameInDEV( | 315 | 307 | type: any, | 316 | | - ownerFn: void | null | Function, | 317 | 308 | currentDispatcherRef: CurrentDispatcherRef, | 318 | 309 | ): string { | 319 | 310 | if (!__DEV__) { | @@ -330,31 +321,29 @@ export function describeUnknownElementTypeFrameInDEV( | 330 | 321 | ); | 331 | 322 | } | 332 | 323 | if (typeof type === 'string') { | 333 | | - return describeBuiltInComponentFrame(type, ownerFn); | | 324 | + return describeBuiltInComponentFrame(type); | 334 | 325 | } | 335 | 326 | switch (type) { | 336 | 327 | case SUSPENSE_NUMBER: | 337 | 328 | case SUSPENSE_SYMBOL_STRING: | 338 | | - return describeBuiltInComponentFrame('Suspense', ownerFn); | | 329 | + return describeBuiltInComponentFrame('Suspense'); | 339 | 330 | case SUSPENSE_LIST_NUMBER: | 340 | 331 | case SUSPENSE_LIST_SYMBOL_STRING: | 341 | | - return describeBuiltInComponentFrame('SuspenseList', ownerFn); | | 332 | + return describeBuiltInComponentFrame('SuspenseList'); | 342 | 333 | } | 343 | 334 | if (typeof type === 'object') { | 344 | 335 | switch (type.$$typeof) { | 345 | 336 | case FORWARD_REF_NUMBER: | 346 | 337 | case FORWARD_REF_SYMBOL_STRING: | 347 | 338 | return describeFunctionComponentFrame( | 348 | 339 | type.render, | 349 | | - ownerFn, | 350 | 340 | currentDispatcherRef, | 351 | 341 | ); | 352 | 342 | case MEMO_NUMBER: | 353 | 343 | case MEMO_SYMBOL_STRING: | 354 | 344 | // Memo may contain any component type so we recursively resolve it. | 355 | 345 | return describeUnknownElementTypeFrameInDEV( | 356 | 346 | type.type, | 357 | | - ownerFn, | 358 | 347 | currentDispatcherRef, | 359 | 348 | ); | 360 | 349 | case LAZY_NUMBER: | @@ -366,7 +355,6 @@ export function describeUnknownElementTypeFrameInDEV( | 366 | 355 | // Lazy may contain any component type so we recursively resolve it. | 367 | 356 | return describeUnknownElementTypeFrameInDEV( | 368 | 357 | init(payload), | 369 | | - ownerFn, | 370 | 358 | currentDispatcherRef, | 371 | 359 | ); | 372 | 360 | } catch (x) {} | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/DevToolsFiberComponentStack.js +4-12Lines changed: 4 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,38 +39,30 @@ export function describeFiber( | 39 | 39 | ClassComponent, | 40 | 40 | } = workTagMap; | 41 | 41 |
| 42 | | - const owner: null | Function = __DEV__ | 43 | | - ? workInProgress._debugOwner | 44 | | - ? workInProgress._debugOwner.type | 45 | | - : null | 46 | | - : null; | 47 | 42 | switch (workInProgress.tag) { | 48 | 43 | case HostComponent: | 49 | | - return describeBuiltInComponentFrame(workInProgress.type, owner); | | 44 | + return describeBuiltInComponentFrame(workInProgress.type); | 50 | 45 | case LazyComponent: | 51 | | - return describeBuiltInComponentFrame('Lazy', owner); | | 46 | + return describeBuiltInComponentFrame('Lazy'); | 52 | 47 | case SuspenseComponent: | 53 | | - return describeBuiltInComponentFrame('Suspense', owner); | | 48 | + return describeBuiltInComponentFrame('Suspense'); | 54 | 49 | case SuspenseListComponent: | 55 | | - return describeBuiltInComponentFrame('SuspenseList', owner); | | 50 | + return describeBuiltInComponentFrame('SuspenseList'); | 56 | 51 | case FunctionComponent: | 57 | 52 | case IndeterminateComponent: | 58 | 53 | case SimpleMemoComponent: | 59 | 54 | return describeFunctionComponentFrame( | 60 | 55 | workInProgress.type, | 61 | | - owner, | 62 | 56 | currentDispatcherRef, | 63 | 57 | ); | 64 | 58 | case ForwardRef: | 65 | 59 | return describeFunctionComponentFrame( | 66 | 60 | workInProgress.type.render, | 67 | | - owner, | 68 | 61 | currentDispatcherRef, | 69 | 62 | ); | 70 | 63 | case ClassComponent: | 71 | 64 | return describeClassComponentFrame( | 72 | 65 | workInProgress.type, | 73 | | - owner, | 74 | 66 | currentDispatcherRef, | 75 | 67 | ); | 76 | 68 | default: | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/renderer.js +35-18Lines changed: 35 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1952,15 +1952,24 @@ export function attach( | 1952 | 1952 | const {key} = fiber; | 1953 | 1953 | const displayName = getDisplayNameForFiber(fiber); | 1954 | 1954 | const elementType = getElementTypeForFiber(fiber); | 1955 | | - const {_debugOwner} = fiber; | | 1955 | + const debugOwner = fiber._debugOwner; | 1956 | 1956 |
| 1957 | 1957 | // Ideally we should call getFiberIDThrows() for _debugOwner, | 1958 | 1958 | // since owners are almost always higher in the tree (and so have already been processed), | 1959 | 1959 | // but in some (rare) instances reported in open source, a descendant mounts before an owner. | 1960 | 1960 | // Since this is a DEV only field it's probably okay to also just lazily generate and ID here if needed. | 1961 | 1961 | // See https://github.com/facebook/react/issues/21445 | 1962 | | - const ownerID = | 1963 | | - _debugOwner != null ? getOrGenerateFiberID(_debugOwner) : 0; | | 1962 | + let ownerID: number; | | 1963 | + if (debugOwner != null) { | | 1964 | + if (typeof debugOwner.tag === 'number') { | | 1965 | + ownerID = getOrGenerateFiberID((debugOwner: any)); | | 1966 | + } else { | | 1967 | + // TODO: Track Server Component Owners. | | 1968 | + ownerID = 0; | | 1969 | + } | | 1970 | + } else { | | 1971 | + ownerID = 0; | | 1972 | + } | 1964 | 1973 | const parentID = parentFiber ? getFiberIDThrows(parentFiber) : 0; | 1965 | 1974 |
| 1966 | 1975 | const displayNameStringID = getStringID(displayName); | @@ -3104,15 +3113,17 @@ export function attach( | 3104 | 3113 | return null; | 3105 | 3114 | } | 3106 | 3115 |
| 3107 | | - const {_debugOwner} = fiber; | 3108 | | - | 3109 | 3116 | const owners: Array<SerializedElement> = [fiberToSerializedElement(fiber)]; | 3110 | 3117 |
| 3111 | | - if (_debugOwner) { | 3112 | | - let owner: null | Fiber = _debugOwner; | 3113 | | - while (owner !== null) { | 3114 | | - owners.unshift(fiberToSerializedElement(owner)); | 3115 | | - owner = owner._debugOwner || null; | | 3118 | + let owner = fiber._debugOwner; | | 3119 | + while (owner != null) { | | 3120 | + if (typeof owner.tag === 'number') { | | 3121 | + const ownerFiber: Fiber = (owner: any); // Refined | | 3122 | + owners.unshift(fiberToSerializedElement(ownerFiber)); | | 3123 | + owner = ownerFiber._debugOwner; | | 3124 | + } else { | | 3125 | + // TODO: Track Server Component Owners. | | 3126 | + break; | 3116 | 3127 | } | 3117 | 3128 | } | 3118 | 3129 |
| @@ -3173,7 +3184,7 @@ export function attach( | 3173 | 3184 | } | 3174 | 3185 |
| 3175 | 3186 | const { | 3176 | | - _debugOwner, | | 3187 | + _debugOwner: debugOwner, | 3177 | 3188 | stateNode, | 3178 | 3189 | key, | 3179 | 3190 | memoizedProps, | @@ -3300,13 +3311,19 @@ export function attach( | 3300 | 3311 | context = {value: context}; | 3301 | 3312 | } | 3302 | 3313 |
| 3303 | | - let owners = null; | 3304 | | - if (_debugOwner) { | 3305 | | - owners = ([]: Array<SerializedElement>); | 3306 | | - let owner: null | Fiber = _debugOwner; | 3307 | | - while (owner !== null) { | 3308 | | - owners.push(fiberToSerializedElement(owner)); | 3309 | | - owner = owner._debugOwner || null; | | 3314 | + let owners: null | Array<SerializedElement> = null; | | 3315 | + let owner = debugOwner; | | 3316 | + while (owner != null) { | | 3317 | + if (typeof owner.tag === 'number') { | | 3318 | + const ownerFiber: Fiber = (owner: any); // Refined | | 3319 | + if (owners === null) { | | 3320 | + owners = []; | | 3321 | + } | | 3322 | + owners.push(fiberToSerializedElement(ownerFiber)); | | 3323 | + owner = ownerFiber._debugOwner; | | 3324 | + } else { | | 3325 | + // TODO: Track Server Component Owners. | | 3326 | + break; | 3310 | 3327 | } | 3311 | 3328 | } | 3312 | 3329 |
| |
|