Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools bugfix: Ignore duplicate welcome "message" events #24186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above change seems kind of silly, since we're removing the event handler here– but it seems like the batching (mentioned in the comment above) prevents the event handler from being removed properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like maybe the removeEventHandler part of the event handler API is being broken– such that we can register an event handler on the window but then never remove it. This is a pretty serious bug that should be fixed (somewhere in react-easy-state maybe?)


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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change wasn't necessary, the name evt just irked me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

}
}

Expand Down