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+
3549window . 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
3953window . addEventListener ( 'pagehide' , function ( { target} ) {
4054 if ( target !== window . document ) {
0 commit comments