Skip to content

Commit bd245c1

Browse files
ChrisDobbybvaughn
andauthored
Ensure sync-xhr is allowed before reload and profile (#20879)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
1 parent b7e6310 commit bd245c1

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
RendererInterface,
3939
} from './types';
4040
import type {ComponentFilter} from '../types';
41+
import {isSynchronousXHRSupported} from './utils';
4142

4243
const debug = (methodName, ...args) => {
4344
if (__DEBUG__) {
@@ -221,6 +222,7 @@ export default class Agent extends EventEmitter<{|
221222
isBackendStorageAPISupported = true;
222223
} catch (error) {}
223224
bridge.send('isBackendStorageAPISupported', isBackendStorageAPISupported);
225+
bridge.send('isSynchronousXHRSupported', isSynchronousXHRSupported());
224226

225227
setupHighlighter(bridge, this);
226228
setupTraceUpdates(this);

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

+8
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,11 @@ export function format(
183183

184184
return '' + formatted;
185185
}
186+
187+
export function isSynchronousXHRSupported(): boolean {
188+
return !!(
189+
window.document &&
190+
window.document.featurePolicy &&
191+
window.document.featurePolicy.allowsFeature('sync-xhr')
192+
);
193+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export type BackendEvents = {|
122122
extensionBackendInitialized: [],
123123
inspectedElement: [InspectedElementPayload],
124124
isBackendStorageAPISupported: [boolean],
125+
isSynchronousXHRSupported: [boolean],
125126
operations: [Array<number>],
126127
ownersList: [OwnersList],
127128
overrideComponentFilters: [Array<ComponentFilter>],

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

+47-9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export default class Store extends EventEmitter<{|
111111
// If not, features like reload-and-profile will not work correctly and must be disabled.
112112
_isBackendStorageAPISupported: boolean = false;
113113

114+
// Can DevTools use sync XHR requests?
115+
// If not, features like reload-and-profile will not work correctly and must be disabled.
116+
// This current limitation applies only to web extension builds
117+
// and will need to be reconsidered in the future if we add support for reload to React Native.
118+
_isSynchronousXHRSupported: boolean = false;
119+
114120
_nativeStyleEditorValidAttributes: $ReadOnlyArray<string> | null = null;
115121

116122
// Map of element (id) to the set of elements (ids) it owns.
@@ -195,12 +201,16 @@ export default class Store extends EventEmitter<{|
195201
bridge.addListener('shutdown', this.onBridgeShutdown);
196202
bridge.addListener(
197203
'isBackendStorageAPISupported',
198-
this.onBridgeStorageSupported,
204+
this.onBackendStorageAPISupported,
199205
);
200206
bridge.addListener(
201207
'isNativeStyleEditorSupported',
202208
this.onBridgeNativeStyleEditorSupported,
203209
);
210+
bridge.addListener(
211+
'isSynchronousXHRSupported',
212+
this.onBridgeSynchronousXHRSupported,
213+
);
204214
bridge.addListener(
205215
'unsupportedRendererVersion',
206216
this.onBridgeUnsupportedRendererVersion,
@@ -359,11 +369,16 @@ export default class Store extends EventEmitter<{|
359369
get supportsProfiling(): boolean {
360370
return this._supportsProfiling;
361371
}
372+
362373
get supportsReloadAndProfile(): boolean {
363374
// Does the DevTools shell support reloading and eagerly injecting the renderer interface?
364-
// And if so, can the backend use the localStorage API?
365-
// Both of these are required for the reload-and-profile feature to work.
366-
return this._supportsReloadAndProfile && this._isBackendStorageAPISupported;
375+
// And if so, can the backend use the localStorage API and sync XHR?
376+
// All of these are currently required for the reload-and-profile feature to work.
377+
return (
378+
this._supportsReloadAndProfile &&
379+
this._isBackendStorageAPISupported &&
380+
this._isSynchronousXHRSupported
381+
);
367382
}
368383

369384
get supportsTraceUpdates(): boolean {
@@ -1130,20 +1145,43 @@ export default class Store extends EventEmitter<{|
11301145
debug('onBridgeShutdown', 'unsubscribing from Bridge');
11311146
}
11321147

1133-
this._bridge.removeListener('operations', this.onBridgeOperations);
1134-
this._bridge.removeListener('shutdown', this.onBridgeShutdown);
1135-
this._bridge.removeListener(
1148+
const bridge = this._bridge;
1149+
bridge.removeListener('operations', this.onBridgeOperations);
1150+
bridge.removeListener(
1151+
'overrideComponentFilters',
1152+
this.onBridgeOverrideComponentFilters,
1153+
);
1154+
bridge.removeListener('shutdown', this.onBridgeShutdown);
1155+
bridge.removeListener(
11361156
'isBackendStorageAPISupported',
1137-
this.onBridgeStorageSupported,
1157+
this.onBackendStorageAPISupported,
1158+
);
1159+
bridge.removeListener(
1160+
'isNativeStyleEditorSupported',
1161+
this.onBridgeNativeStyleEditorSupported,
1162+
);
1163+
bridge.removeListener(
1164+
'isSynchronousXHRSupported',
1165+
this.onBridgeSynchronousXHRSupported,
1166+
);
1167+
bridge.removeListener(
1168+
'unsupportedRendererVersion',
1169+
this.onBridgeUnsupportedRendererVersion,
11381170
);
11391171
};
11401172

1141-
onBridgeStorageSupported = (isBackendStorageAPISupported: boolean) => {
1173+
onBackendStorageAPISupported = (isBackendStorageAPISupported: boolean) => {
11421174
this._isBackendStorageAPISupported = isBackendStorageAPISupported;
11431175

11441176
this.emit('supportsReloadAndProfile');
11451177
};
11461178

1179+
onBridgeSynchronousXHRSupported = (isSynchronousXHRSupported: boolean) => {
1180+
this._isSynchronousXHRSupported = isSynchronousXHRSupported;
1181+
1182+
this.emit('supportsReloadAndProfile');
1183+
};
1184+
11471185
onBridgeUnsupportedRendererVersion = () => {
11481186
this._unsupportedRendererVersionDetected = true;
11491187

0 commit comments

Comments
 (0)