Skip to content

Commit 0ac68a9

Browse files
committed
Track byteSize as part of ReactIOInfo
This is mainly intended to be used for client-side resources like img and css in this case.
1 parent 2ba7b07 commit 0ac68a9

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

packages/react-devtools-shared/src/backend/fiber/renderer.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,7 @@ export function attach(
33153315
}
33163316
let start = -1;
33173317
let end = -1;
3318+
let byteSize = 0;
33183319
// $FlowFixMe[method-unbinding]
33193320
if (typeof performance.getEntriesByType === 'function') {
33203321
// We may be able to collect the start and end time of this resource from Performance Observer.
@@ -3324,6 +3325,8 @@ export function attach(
33243325
if (resourceEntry.name === href) {
33253326
start = resourceEntry.startTime;
33263327
end = start + resourceEntry.duration;
3328+
// $FlowFixMe[prop-missing]
3329+
byteSize = (resourceEntry.encodedBodySize: any) || 0;
33273330
}
33283331
}
33293332
}
@@ -3339,6 +3342,10 @@ export function attach(
33393342
// $FlowFixMe: This field doesn't usually take a Fiber but we're only using inside this file.
33403343
owner: fiber, // Allow linking to the <link> if it's not filtered.
33413344
};
3345+
if (byteSize > 0) {
3346+
// $FlowFixMe[cannot-write]
3347+
ioInfo.byteSize = byteSize;
3348+
}
33423349
const asyncInfo: ReactAsyncInfo = {
33433350
awaited: ioInfo,
33443351
// $FlowFixMe: This field doesn't usually take a Fiber but we're only using inside this file.
@@ -3403,7 +3410,7 @@ export function attach(
34033410
}
34043411
let start = -1;
34053412
let end = -1;
3406-
let fileSize = 0;
3413+
let byteSize = 0;
34073414
// $FlowFixMe[method-unbinding]
34083415
if (typeof performance.getEntriesByType === 'function') {
34093416
// We may be able to collect the start and end time of this resource from Performance Observer.
@@ -3414,7 +3421,7 @@ export function attach(
34143421
start = resourceEntry.startTime;
34153422
end = start + resourceEntry.duration;
34163423
// $FlowFixMe[prop-missing]
3417-
fileSize = (resourceEntry.encodedBodySize: any) || 0;
3424+
byteSize = (resourceEntry.encodedBodySize: any) || 0;
34183425
}
34193426
}
34203427
}
@@ -3433,10 +3440,6 @@ export function attach(
34333440
value.naturalWidth = instance.naturalWidth;
34343441
value.naturalHeight = instance.naturalHeight;
34353442
}
3436-
if (fileSize > 0) {
3437-
// Cross-origin images won't have a file size that we can access.
3438-
value.fileSize = fileSize;
3439-
}
34403443
const promise = Promise.resolve(value);
34413444
(promise: any).status = 'fulfilled';
34423445
(promise: any).value = value;
@@ -3448,6 +3451,12 @@ export function attach(
34483451
// $FlowFixMe: This field doesn't usually take a Fiber but we're only using inside this file.
34493452
owner: fiber, // Allow linking to the <link> if it's not filtered.
34503453
};
3454+
if (byteSize > 0) {
3455+
// Cross-origin images won't have a file size that we can access.
3456+
value.fileSize = byteSize;
3457+
// $FlowFixMe[cannot-write]
3458+
ioInfo.byteSize = byteSize;
3459+
}
34513460
const asyncInfo: ReactAsyncInfo = {
34523461
awaited: ioInfo,
34533462
// $FlowFixMe: This field doesn't usually take a Fiber but we're only using inside this file.
@@ -5778,6 +5787,7 @@ export function attach(
57785787
description: getIODescription(resolvedValue),
57795788
start: ioInfo.start,
57805789
end: ioInfo.end,
5790+
byteSize: ioInfo.byteSize == null ? null : ioInfo.byteSize,
57815791
value: ioInfo.value == null ? null : ioInfo.value,
57825792
env: ioInfo.env == null ? null : ioInfo.env,
57835793
owner:

packages/react-devtools-shared/src/backend/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export type SerializedIOInfo = {
238238
description: string,
239239
start: number,
240240
end: number,
241+
byteSize: null | number,
241242
value: null | Promise<mixed>,
242243
env: null | string,
243244
owner: null | SerializedElement,

packages/react-devtools-shared/src/backendAPI.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ function backendToFrontendSerializedAsyncInfo(
221221
description: ioInfo.description,
222222
start: ioInfo.start,
223223
end: ioInfo.end,
224+
byteSize: ioInfo.byteSize,
224225
value: ioInfo.value,
225226
env: ioInfo.env,
226227
owner:

packages/react-devtools-shared/src/devtools/views/Components/InspectedElementSuspendedBy.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ function getShortDescription(name: string, description: string): string {
6969
return '';
7070
}
7171

72+
function formatBytes(bytes: number) {
73+
if (bytes < 1_000) {
74+
return bytes + ' bytes';
75+
}
76+
if (bytes < 1_000_000) {
77+
return (bytes / 1_000).toFixed(1) + ' kB';
78+
}
79+
if (bytes < 1_000_000_000) {
80+
return (bytes / 1_000_000).toFixed(1) + ' mB';
81+
}
82+
return (bytes / 1_000_000_000).toFixed(1) + ' gB';
83+
}
84+
7285
function SuspendedByRow({
7386
bridge,
7487
element,
@@ -138,7 +151,13 @@ function SuspendedByRow({
138151
<Button
139152
className={styles.CollapsableHeader}
140153
onClick={() => setIsOpen(prevIsOpen => !prevIsOpen)}
141-
title={longName + ' — ' + (end - start).toFixed(2) + ' ms'}>
154+
title={
155+
longName +
156+
' — ' +
157+
(end - start).toFixed(2) +
158+
' ms' +
159+
(ioInfo.byteSize != null ? ' — ' + formatBytes(ioInfo.byteSize) : '')
160+
}>
142161
<ButtonIcon
143162
className={styles.CollapsableHeaderIcon}
144163
type={isOpen ? 'expanded' : 'collapsed'}

packages/react-devtools-shared/src/frontend/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export type SerializedIOInfo = {
206206
description: string,
207207
start: number,
208208
end: number,
209+
byteSize: null | number,
209210
value: null | Promise<mixed>,
210211
env: null | string,
211212
owner: null | SerializedElement,

packages/shared/ReactTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export type ReactIOInfo = {
237237
+name: string, // the name of the async function being called (e.g. "fetch")
238238
+start: number, // the start time
239239
+end: number, // the end time (this might be different from the time the await was unblocked)
240+
+byteSize?: number, // the byte size of this resource across the network. (should only be included if affecting the client.)
240241
+value?: null | Promise<mixed>, // the Promise that was awaited if any, may be rejected
241242
+env?: string, // the environment where this I/O was spawned.
242243
+owner?: null | ReactComponentInfo,

0 commit comments

Comments
 (0)