Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08c1f79

Browse files
authored
Fix Cannot read property 'sub' of undefined when navigating to plain-text pages (facebook#17848)
Update various parts of DevTools to account for the fact that the global "hook" might be undefined if DevTools didn't inject it (due to the page's `contentType`) it (due to the page's `contentType`)
1 parent 9ad3590 commit 08c1f79

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

packages/react-devtools-core/src/backend.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type ConnectOptions = {
3232

3333
installHook(window);
3434

35-
const hook: DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
35+
const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
3636

3737
let savedComponentFilters: Array<ComponentFilter> = getDefaultComponentFilters();
3838

@@ -48,6 +48,10 @@ function debug(methodName: string, ...args) {
4848
}
4949

5050
export function connectToDevTools(options: ?ConnectOptions) {
51+
if (hook == null) {
52+
// DevTools didn't get injected into this page (maybe b'c of the contentType).
53+
return;
54+
}
5155
const {
5256
host = 'localhost',
5357
nativeStyleEditorValidAttributes,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function welcome(event) {
2222
window.addEventListener('message', welcome);
2323

2424
function setup(hook) {
25+
if (hook == null) {
26+
// DevTools didn't get injected into this page (maybe b'c of the contentType).
27+
return;
28+
}
2529
const Agent = require('react-devtools-shared/src/backend/agent').default;
2630
const Bridge = require('react-devtools-shared/src/bridge').default;
2731
const {initBackend} = require('react-devtools-shared/src/backend');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') {
8888

8989
// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.
9090
// Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.
91-
if (document.contentType === 'text/html') {
91+
if ('text/html' === document.contentType) {
9292
injectCode(
9393
';(' +
9494
installHook.toString() +

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function createPanelIfReactLoaded() {
271271
// When the user chooses a different node in the browser Elements tab,
272272
// copy it over to the hook object so that we can sync the selection.
273273
chrome.devtools.inspectedWindow.eval(
274-
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
274+
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
275275
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 = $0, true) :' +
276276
'false',
277277
(didSelectionChange, evalError) => {

packages/react-devtools-inline/src/backend.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ function finishActivation(contentWindow: window) {
7373
const agent = new Agent(bridge);
7474

7575
const hook = contentWindow.__REACT_DEVTOOLS_GLOBAL_HOOK__;
76-
77-
initBackend(hook, agent, contentWindow);
78-
79-
// Setup React Native style editor if a renderer like react-native-web has injected it.
80-
if (hook.resolveRNStyle) {
81-
setupNativeStyleEditor(
82-
bridge,
83-
agent,
84-
hook.resolveRNStyle,
85-
hook.nativeStyleEditorValidAttributes,
86-
);
76+
if (hook) {
77+
initBackend(hook, agent, contentWindow);
78+
79+
// Setup React Native style editor if a renderer like react-native-web has injected it.
80+
if (hook.resolveRNStyle) {
81+
setupNativeStyleEditor(
82+
bridge,
83+
agent,
84+
hook.resolveRNStyle,
85+
hook.nativeStyleEditorValidAttributes,
86+
);
87+
}
8788
}
8889
}
8990

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function initBackend(
1919
agent: Agent,
2020
global: Object,
2121
): () => void {
22+
if (hook == null) {
23+
// DevTools didn't get injected into this page (maybe b'c of the contentType).
24+
return () => {};
25+
}
2226
const subs = [
2327
hook.sub(
2428
'renderer-attached',

0 commit comments

Comments
 (0)