Skip to content

Commit 96a3c35

Browse files
author
Brian Vaughn
committed
DevTools bugfix: Ignore duplicate welcome "message" events
1 parent e7d0053 commit 96a3c35

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict';
88

9+
let welcomeHasInitialized;
10+
911
function welcome(event) {
1012
if (
1113
event.source !== window ||
@@ -14,6 +16,21 @@ function welcome(event) {
1416
return;
1517
}
1618

19+
// In some circumstances, this method is called more than once for a single welcome message.
20+
// The exact circumstances of this are unclear, though it may have to do with 3rd part script event batching.
21+
// Regardless, this can cause DevTools to add duplicate elements to the Store (and throw an error)
22+
// or worse yet, choke up entirely and freeze the browser.
23+
// The simplest solution is to ignore the duplicate events.
24+
// See https://github.com/facebook/react/issues/24162
25+
if (welcomeHasInitialized) {
26+
console.warn(
27+
'React DevTools detected duplicate welcome "message" events from the content script.',
28+
);
29+
return;
30+
}
31+
32+
welcomeHasInitialized = true;
33+
1734
window.removeEventListener('message', welcome);
1835

1936
setup(window.__REACT_DEVTOOLS_GLOBAL_HOOK__);

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ function handleMessageFromDevtools(message) {
2525
);
2626
}
2727

28-
function handleMessageFromPage(evt) {
28+
function handleMessageFromPage(event) {
2929
if (
30-
evt.source === window &&
31-
evt.data &&
32-
evt.data.source === 'react-devtools-bridge'
30+
event.source === window &&
31+
event.data &&
32+
event.data.source === 'react-devtools-bridge'
3333
) {
3434
backendInitialized = true;
3535

36-
port.postMessage(evt.data.payload);
36+
port.postMessage(event.data.payload);
3737
}
3838
}
3939

0 commit comments

Comments
 (0)