Skip to content

Commit

Permalink
DevTools bugfix: Ignore duplicate welcome "message" events
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Mar 28, 2022
1 parent e7d0053 commit 96a3c35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 17 additions & 0 deletions packages/react-devtools-extensions/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

'use strict';

let welcomeHasInitialized;

function welcome(event) {
if (
event.source !== window ||
Expand All @@ -14,6 +16,21 @@ function welcome(event) {
return;
}

// In some circumstances, this method is called more than once for a single welcome message.
// The exact circumstances of this are unclear, though it may have to do with 3rd part script event batching.
// Regardless, this can cause DevTools to add duplicate elements to the Store (and throw an error)
// or worse yet, choke up entirely and freeze the browser.
// The simplest solution is to ignore the duplicate events.
// See https://github.com/facebook/react/issues/24162
if (welcomeHasInitialized) {
console.warn(
'React DevTools detected duplicate welcome "message" events from the content script.',
);
return;
}

welcomeHasInitialized = true;

window.removeEventListener('message', welcome);

setup(window.__REACT_DEVTOOLS_GLOBAL_HOOK__);
Expand Down
10 changes: 5 additions & 5 deletions packages/react-devtools-extensions/src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ function handleMessageFromDevtools(message) {
);
}

function handleMessageFromPage(evt) {
function handleMessageFromPage(event) {
if (
evt.source === window &&
evt.data &&
evt.data.source === 'react-devtools-bridge'
event.source === window &&
event.data &&
event.data.source === 'react-devtools-bridge'
) {
backendInitialized = true;

port.postMessage(evt.data.payload);
port.postMessage(event.data.payload);
}
}

Expand Down

0 comments on commit 96a3c35

Please sign in to comment.