Skip to content

Commit

Permalink
Fix Cannot read property 'sub' of undefined when navigating to plain-…
Browse files Browse the repository at this point in the history
…text pages (#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`)
  • Loading branch information
wfnuser authored Feb 2, 2020
1 parent 9ad3590 commit 08c1f79
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
6 changes: 5 additions & 1 deletion packages/react-devtools-core/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ConnectOptions = {

installHook(window);

const hook: DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

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

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

export function connectToDevTools(options: ?ConnectOptions) {
if (hook == null) {
// DevTools didn't get injected into this page (maybe b'c of the contentType).
return;
}
const {
host = 'localhost',
nativeStyleEditorValidAttributes,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-devtools-extensions/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function welcome(event) {
window.addEventListener('message', welcome);

function setup(hook) {
if (hook == null) {
// DevTools didn't get injected into this page (maybe b'c of the contentType).
return;
}
const Agent = require('react-devtools-shared/src/backend/agent').default;
const Bridge = require('react-devtools-shared/src/bridge').default;
const {initBackend} = require('react-devtools-shared/src/backend');
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/src/injectGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') {

// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.
// Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.
if (document.contentType === 'text/html') {
if ('text/html' === document.contentType) {
injectCode(
';(' +
installHook.toString() +
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function createPanelIfReactLoaded() {
// When the user chooses a different node in the browser Elements tab,
// copy it over to the hook object so that we can sync the selection.
chrome.devtools.inspectedWindow.eval(
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 = $0, true) :' +
'false',
(didSelectionChange, evalError) => {
Expand Down
23 changes: 12 additions & 11 deletions packages/react-devtools-inline/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ function finishActivation(contentWindow: window) {
const agent = new Agent(bridge);

const hook = contentWindow.__REACT_DEVTOOLS_GLOBAL_HOOK__;

initBackend(hook, agent, contentWindow);

// Setup React Native style editor if a renderer like react-native-web has injected it.
if (hook.resolveRNStyle) {
setupNativeStyleEditor(
bridge,
agent,
hook.resolveRNStyle,
hook.nativeStyleEditorValidAttributes,
);
if (hook) {
initBackend(hook, agent, contentWindow);

// Setup React Native style editor if a renderer like react-native-web has injected it.
if (hook.resolveRNStyle) {
setupNativeStyleEditor(
bridge,
agent,
hook.resolveRNStyle,
hook.nativeStyleEditorValidAttributes,
);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/react-devtools-shared/src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export function initBackend(
agent: Agent,
global: Object,
): () => void {
if (hook == null) {
// DevTools didn't get injected into this page (maybe b'c of the contentType).
return () => {};
}
const subs = [
hook.sub(
'renderer-attached',
Expand Down

0 comments on commit 08c1f79

Please sign in to comment.