@@ -59,7 +59,7 @@ function setupPageNavigationListeners(): void {
5959}
6060
6161let isPageLifecycleInitialized = false ;
62- function initializePageEventListeners ( ) : void {
62+ function onPageReady ( callback : ( ) => void ) {
6363 if ( typeof window === 'undefined' ) return ;
6464
6565 if ( isPageLifecycleInitialized ) {
@@ -68,9 +68,12 @@ function initializePageEventListeners(): void {
6868 isPageLifecycleInitialized = true ;
6969
7070 if ( document . readyState === 'complete' ) {
71- setupPageNavigationListeners ( ) ;
71+ callback ( ) ;
7272 } else {
73- document . addEventListener ( 'load' , setupPageNavigationListeners ) ;
73+ document . addEventListener ( 'readystatechange' , function onReadyStateChange ( ) {
74+ onPageReady ( callback ) ;
75+ document . removeEventListener ( 'readystatechange' , onReadyStateChange ) ;
76+ } ) ;
7477 }
7578}
7679
@@ -79,13 +82,13 @@ export function onPageLoaded(callback: PageLifecycleCallback): void {
7982 void callback ( ) ;
8083 }
8184 pageLoadedCallbacks . add ( callback ) ;
82- initializePageEventListeners ( ) ;
85+ onPageReady ( setupPageNavigationListeners ) ;
8386}
8487
8588export function onPageUnloaded ( callback : PageLifecycleCallback ) : void {
8689 if ( currentPageState === 'unload' ) {
8790 void callback ( ) ;
8891 }
8992 pageUnloadedCallbacks . add ( callback ) ;
90- initializePageEventListeners ( ) ;
93+ onPageReady ( setupPageNavigationListeners ) ;
9194}
0 commit comments