Skip to content

Commit 497c5d1

Browse files
committed
revert to original change
1 parent c63c292 commit 497c5d1

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/react-on-rails/src/pageLifecycle.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function setupPageNavigationListeners(): void {
5959
}
6060

6161
let 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

8588
export function onPageUnloaded(callback: PageLifecycleCallback): void {
8689
if (currentPageState === 'unload') {
8790
void callback();
8891
}
8992
pageUnloadedCallbacks.add(callback);
90-
initializePageEventListeners();
93+
onPageReady(setupPageNavigationListeners);
9194
}

packages/react-on-rails/tests/pageLifecycle.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('pageLifecycle', () => {
9191
// Should not call callback immediately since readyState is 'loading'
9292
expect(callback).not.toHaveBeenCalled();
9393
// Verify that a DOMContentLoaded listener was added when readyState is 'loading'
94-
expect(addEventListenerSpy).toHaveBeenCalledWith('load', expect.any(Function));
94+
expect(addEventListenerSpy).toHaveBeenCalledWith('readystatechange', expect.any(Function));
9595
});
9696

9797
it('should wait for readystatechange when document.readyState is "loading"', () => {
@@ -104,7 +104,7 @@ describe('pageLifecycle', () => {
104104
// Should not call callback immediately since readyState is 'loading'
105105
expect(callback).not.toHaveBeenCalled();
106106
// Verify that a DOMContentLoaded listener was added when readyState is 'loading'
107-
expect(addEventListenerSpy).toHaveBeenCalledWith('load', expect.any(Function));
107+
expect(addEventListenerSpy).toHaveBeenCalledWith('readystatechange', expect.any(Function));
108108
});
109109

110110
describe('with Turbo navigation library', () => {

0 commit comments

Comments
 (0)