Skip to content

Commit 377c517

Browse files
authored
DevTools: fix backend activation (#26779)
## Summary We have a case: 1. Open components tab 2. Close Chrome / Firefox devtools window completely 3. Reopen browser devtools panel 4. Open components tab Currently, in version 4.27.6, we cannot load the components tree. This PR contains two changes: - non-functional refactoring in `react-devtools-shared/src/devtools/store.js`: removed some redundant type castings. - fixed backend manager logic (introduced in #26615) to activate already registered backends. Looks like frontend of devtools also depends on `renderer-attached` event, without it component tree won't load. ## How did you test this change? This fixes the case mentioned prior. Currently in 4.27.6 version it is not working, we need to refresh the page to make it work. I've tested this in several environments: chrome, firefox, standalone with RN application.
1 parent aef7ce5 commit 377c517

File tree

5 files changed

+82
-66
lines changed

5 files changed

+82
-66
lines changed

packages/react-devtools-extensions/src/backendManager.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ function setup(hook: ?DevToolsHook) {
6161
hook.renderers.forEach(renderer => {
6262
registerRenderer(renderer);
6363
});
64+
65+
// Activate and remove from required all present backends, registered within the hook
66+
hook.backends.forEach((_, backendVersion) => {
67+
requiredBackends.delete(backendVersion);
68+
activateBackend(backendVersion, hook);
69+
});
70+
6471
updateRequiredBackends();
6572

6673
// register renderers that inject themselves later.

packages/react-devtools-extensions/src/background.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ async function dynamicallyInjectContentScripts() {
2828
// For some reason dynamically injected scripts might be already registered
2929
// Registering them again will fail, which will result into
3030
// __REACT_DEVTOOLS_GLOBAL_HOOK__ hook not being injected
31-
await chrome.scripting.unregisterContentScripts({
32-
ids: contentScriptsToInject.map(s => s.id),
33-
});
31+
32+
// Not specifying ids, because Chrome throws an error
33+
// if id of non-injected script is provided
34+
await chrome.scripting.unregisterContentScripts();
3435

3536
// equivalent logic for Firefox is in prepareInjection.js
3637
// Manifest V3 method of injecting content script

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {hasAssignedBackend} from './utils';
1515

1616
import type {DevToolsHook, ReactRenderer, RendererInterface} from './types';
1717

18-
// this is the backend that is compactible with all older React versions
18+
// this is the backend that is compatible with all older React versions
1919
function isMatchingRender(version: string): boolean {
2020
return !hasAssignedBackend(version);
2121
}
@@ -31,6 +31,7 @@ export function initBackend(
3131
// DevTools didn't get injected into this page (maybe b'c of the contentType).
3232
return () => {};
3333
}
34+
3435
const subs = [
3536
hook.sub(
3637
'renderer-attached',
@@ -64,10 +65,6 @@ export function initBackend(
6465
];
6566

6667
const attachRenderer = (id: number, renderer: ReactRenderer) => {
67-
// skip if already attached
68-
if (renderer.attached) {
69-
return;
70-
}
7168
// only attach if the renderer is compatible with the current version of the backend
7269
if (!isMatchingRender(renderer.reconcilerVersion || renderer.version)) {
7370
return;
@@ -102,7 +99,6 @@ export function initBackend(
10299
} else {
103100
hook.emit('unsupported-renderer-version', id);
104101
}
105-
renderer.attached = true;
106102
};
107103

108104
// Connect renderers that have already injected themselves.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ export type ReactRenderer = {
171171
// 18.0+
172172
injectProfilingHooks?: (profilingHooks: DevToolsProfilingHooks) => void,
173173
getLaneLabelMap?: () => Map<Lane, string> | null,
174-
// set by backend after successful attaching
175-
attached?: boolean,
176174
...
177175
};
178176

0 commit comments

Comments
 (0)