Skip to content

Commit

Permalink
DevTools bugfix: Ignore duplicate welcome "message" events (#24186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn authored and rickhanlonii committed Apr 13, 2022
1 parent 0f044ad commit 46c2f80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 21 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 = false;

function welcome(event) {
if (
event.source !== window ||
Expand All @@ -14,6 +16,25 @@ 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 seems related to 3rd party event batching code.
//
// Regardless, call this method multiple times 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.
// To be clear, this SHOULD NOT BE NECESSARY, since we remove the event handler below.
//
// 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 46c2f80

Please sign in to comment.