Skip to content

Commit ae0dc78

Browse files
committed
[DevTools] Ignore renderers without support for a dedicated Suspense tree
Mostly so that we don't trigger unnecessary warnings in older React versions.
1 parent 755ceba commit ae0dc78

File tree

9 files changed

+36
-6
lines changed

9 files changed

+36
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ export function attach(
10651065
scheduleUpdate,
10661066
getCurrentFiber,
10671067
} = renderer;
1068+
const supportsSuspenseTree = true;
10681069
const supportsTogglingError =
10691070
typeof setErrorHandler === 'function' &&
10701071
typeof scheduleUpdate === 'function';
@@ -2394,6 +2395,7 @@ export function attach(
23942395
);
23952396
pushOperation(hasOwnerMetadata ? 1 : 0);
23962397
pushOperation(supportsTogglingSuspense ? 1 : 0);
2398+
pushOperation(supportsSuspenseTree ? 1 : 0);
23972399
23982400
if (isProfiling) {
23992401
if (displayNamesByRootID !== null) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export function attach(
181181
}
182182

183183
const supportsTogglingSuspense = false;
184+
const supportsSuspenseTree = false;
184185

185186
function getDisplayNameForElementID(id: number): string | null {
186187
const internalInstance = idToInternalInstanceMap.get(id);
@@ -411,6 +412,7 @@ export function attach(
411412
pushOperation(0); // StrictMode supported?
412413
pushOperation(hasOwnerMetadata ? 1 : 0);
413414
pushOperation(supportsTogglingSuspense ? 1 : 0);
415+
pushOperation(supportsSuspenseTree ? 1 : 0);
414416
} else {
415417
const type = getElementType(internalInstance);
416418
const {displayName, key} = getData(internalInstance);

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export type Capabilities = {
9090
supportsBasicProfiling: boolean,
9191
hasOwnerMetadata: boolean,
9292
supportsStrictMode: boolean,
93+
supportsSuspenseTree: boolean,
9394
supportsTogglingSuspense: boolean,
9495
supportsTimeline: boolean,
9596
};
@@ -493,6 +494,14 @@ export default class Store extends EventEmitter<{
493494
);
494495
}
495496

497+
supportsSuspenseTree(rootID: Element['id']): boolean {
498+
const capabilities = this._rootIDToCapabilities.get(rootID);
499+
if (capabilities === undefined) {
500+
throw new Error(`No capabilities registered for root ${rootID}`);
501+
}
502+
return capabilities.supportsSuspenseTree;
503+
}
504+
496505
supportsTogglingSuspense(rootID: Element['id']): boolean {
497506
const capabilities = this._rootIDToCapabilities.get(rootID);
498507
if (capabilities === undefined) {
@@ -895,11 +904,14 @@ export default class Store extends EventEmitter<{
895904
if (root === null) {
896905
return [];
897906
}
898-
if (!this.supportsTogglingSuspense(root.id)) {
907+
if (
908+
!this.supportsTogglingSuspense(rootID) ||
909+
!this.supportsSuspenseTree(rootID)
910+
) {
899911
return [];
900912
}
901913
const list: SuspenseNode['id'][] = [];
902-
const suspense = this.getSuspenseByID(root.id);
914+
const suspense = this.getSuspenseByID(rootID);
903915
if (suspense !== null) {
904916
const stack = [suspense];
905917
while (stack.length > 0) {
@@ -1170,6 +1182,7 @@ export default class Store extends EventEmitter<{
11701182
let supportsStrictMode = false;
11711183
let hasOwnerMetadata = false;
11721184
let supportsTogglingSuspense = false;
1185+
let supportsSuspenseTree = false;
11731186

11741187
// If we don't know the bridge protocol, guess that we're dealing with the latest.
11751188
// If we do know it, we can take it into consideration when parsing operations.
@@ -1185,6 +1198,9 @@ export default class Store extends EventEmitter<{
11851198

11861199
supportsTogglingSuspense = operations[i] > 0;
11871200
i++;
1201+
1202+
supportsSuspenseTree = operations[i] > 0;
1203+
i++;
11881204
}
11891205

11901206
this._roots = this._roots.concat(id);
@@ -1193,6 +1209,7 @@ export default class Store extends EventEmitter<{
11931209
supportsBasicProfiling,
11941210
hasOwnerMetadata,
11951211
supportsStrictMode,
1212+
supportsSuspenseTree,
11961213
supportsTogglingSuspense,
11971214
supportsTimeline,
11981215
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export function printStore(
176176

177177
rootWeight += weight;
178178

179-
if (includeSuspense) {
179+
if (includeSuspense && store.supportsSuspenseTree(rootID)) {
180180
const root = store.getSuspenseByID(rootID);
181181
// Roots from legacy renderers don't have a separate Suspense tree
182182
if (root !== null) {

packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function updateTree(
210210
i++; // supportsStrictMode flag
211211
i++; // hasOwnerMetadata flag
212212
i++; // supportsTogglingSuspense flag
213+
i++; // supportsSuspenseTree flag
213214

214215
if (__DEBUG__) {
215216
debug('Add', `new root fiber ${id}`);

packages/react-devtools-shared/src/devtools/views/SuspenseTab/SuspenseRects.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ function getDocumentBoundingRect(
190190

191191
for (let i = 0; i < roots.length; i++) {
192192
const rootID = roots[i];
193-
const root = store.getSuspenseByID(rootID);
193+
const root = store.supportsSuspenseTree(rootID)
194+
? store.getSuspenseByID(rootID)
195+
: null;
194196
if (root === null) {
195197
continue;
196198
}

packages/react-devtools-shared/src/devtools/views/SuspenseTab/SuspenseTimeline.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ export default function SuspenseTimeline(): React$Node {
243243
const name = '#' + rootID;
244244
// TODO: Highlight host on hover
245245
return (
246-
<option key={rootID} value={rootID}>
246+
<option
247+
key={rootID}
248+
value={rootID}
249+
disabled={!store.supportsSuspenseTree(rootID)}>
247250
{name}
248251
</option>
249252
);

packages/react-devtools-shared/src/devtools/views/SuspenseTab/SuspenseTreeContext.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ type Props = {
8282

8383
function getDefaultRootID(store: Store): Element['id'] | null {
8484
const designatedRootID = store.roots.find(rootID => {
85-
const suspense = store.getSuspenseByID(rootID);
85+
const suspense = store.supportsSuspenseTree(rootID)
86+
? store.getSuspenseByID(rootID)
87+
: null;
8688
return (
8789
store.supportsTogglingSuspense(rootID) &&
8890
suspense !== null &&

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export function printOperationsArray(operations: Array<number>) {
263263
i++; // supportsStrictMode
264264
i++; // hasOwnerMetadata
265265
i++; // supportsTogglingSuspense
266+
i++; // supportsSuspenseTree
266267
} else {
267268
const parentID = ((operations[i]: any): number);
268269
i++;

0 commit comments

Comments
 (0)