[DevTools] Don't connect to pages that are being prerendered (#35958)
Sebastian "Sebbie" Silbermann committed
Mar 4, 2026 at 13:39 UTC
23b2d8514f13f109b980b0a1f4f3aab906ad51d0
2 files changed
+18
-2
flow-typed/environments/dom.js
+2
@@ -1415,6 +1415,8 @@ declare class Document extends Node {
1415
links: HTMLCollection<HTMLLinkElement>;
1416
media: string;
1417
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;
1420
readyState: string;
1421
referrer: string;
1422
scripts: HTMLCollection<HTMLScriptElement>;
packages/react-devtools-extensions/src/contentScripts/proxy.js
+16
-2
@@ -10,7 +10,7 @@
10
11
'use strict';
12
13
-function injectProxy({target}: {target: any}) {
13
+function injectProxy() {
14
// Firefox's behaviour for injecting this content script can be unpredictable
15
// While navigating the history, some content scripts might not be re-injected and still be alive
16
if (!window.__REACT_DEVTOOLS_PROXY_INJECTED__) {
@@ -32,9 +32,23 @@ function injectProxy({target}: {target: any}) {
32
}
33
}
34
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
+
49
window.addEventListener('pagereveal', injectProxy);
50
// For backwards compat with browsers not implementing `pagereveal` which is a fairly new event.
37
-window.addEventListener('pageshow', injectProxy);
51
+window.addEventListener('pageshow', handlePageShow);
52
53
window.addEventListener('pagehide', function ({target}) {
54
if (target !== window.document) {