Skip to content

Commit 23b2d85

Browse files
authored
[DevTools] Don't connect to pages that are being prerendered (facebook#35958)
1 parent 4b568a8 commit 23b2d85

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

  • flow-typed/environments
  • packages/react-devtools-extensions/src/contentScripts

flow-typed/environments/dom.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,8 @@ declare class Document extends Node {
14151415
links: HTMLCollection<HTMLLinkElement>;
14161416
media: string;
14171417
open(url?: string, name?: string, features?: string, replace?: boolean): any;
1418+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/prerendering} */
1419+
prerendering: boolean;
14181420
readyState: string;
14191421
referrer: string;
14201422
scripts: HTMLCollection<HTMLScriptElement>;

packages/react-devtools-extensions/src/contentScripts/proxy.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use strict';
1212

13-
function injectProxy({target}: {target: any}) {
13+
function injectProxy() {
1414
// Firefox's behaviour for injecting this content script can be unpredictable
1515
// While navigating the history, some content scripts might not be re-injected and still be alive
1616
if (!window.__REACT_DEVTOOLS_PROXY_INJECTED__) {
@@ -32,9 +32,23 @@ function injectProxy({target}: {target: any}) {
3232
}
3333
}
3434

35+
function handlePageShow() {
36+
if (document.prerendering) {
37+
// React DevTools can't handle multiple documents being connected to the same extension port.
38+
// However, browsers are firing pageshow events while prerendering (https://issues.chromium.org/issues/489633225).
39+
// We need to wait until prerendering is finished before injecting the proxy.
40+
// In browsers with pagereveal support, listening to pagereveal would be sufficient.
41+
// Waiting for prerenderingchange is a workaround to support browsers that
42+
// have speculationrules but not pagereveal.
43+
document.addEventListener('prerenderingchange', injectProxy, {once: true});
44+
} else {
45+
injectProxy();
46+
}
47+
}
48+
3549
window.addEventListener('pagereveal', injectProxy);
3650
// For backwards compat with browsers not implementing `pagereveal` which is a fairly new event.
37-
window.addEventListener('pageshow', injectProxy);
51+
window.addEventListener('pageshow', handlePageShow);
3852

3953
window.addEventListener('pagehide', function ({target}) {
4054
if (target !== window.document) {

0 commit comments

Comments
 (0)