Skip to content

Commit 322e182

Browse files
justin808claude
andcommitted
Improve naming clarity in pageLifecycle.ts
The main improvements: - Rename setupTurbolinksEventListeners() to setupPageNavigationListeners() to better reflect that it handles multiple navigation libraries, not just Turbolinks - Rename isEventListenerInitialized to isPageLifecycleInitialized for clarity - Extract hasNavigationLibrary variable to make the conditional logic clearer - Update debug messages to be more descriptive ("DETECTED" vs "USING") - Emphasize that the normal use case is NO navigation library These changes make the code more self-documenting and easier to understand, especially given that most users don't use Turbolinks at all. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 171c7ea commit 322e182

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

node_package/src/pageLifecycle.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,49 @@ function runPageUnloadedCallbacks(): void {
2828
});
2929
}
3030

31-
function setupTurbolinksEventListeners(): void {
31+
function setupPageNavigationListeners(): void {
3232
// Install listeners when running on the client (browser).
33-
// We must do this check for turbolinks AFTER the document is loaded because we load the
33+
// We must check for navigation libraries AFTER the document is loaded because we load the
3434
// Webpack bundles first.
35-
if ((!turbolinksInstalled() || !turbolinksSupported()) && !turboInstalled()) {
36-
debugTurbolinks('NOT USING TURBOLINKS: calling reactOnRailsPageLoaded');
35+
const hasNavigationLibrary = (turbolinksInstalled() && turbolinksSupported()) || turboInstalled();
36+
if (!hasNavigationLibrary) {
37+
debugTurbolinks('NO NAVIGATION LIBRARY: running page loaded callbacks immediately');
3738
runPageLoadedCallbacks();
3839
return;
3940
}
4041

4142
if (turboInstalled()) {
42-
debugTurbolinks('USING TURBO: document added event listeners turbo:before-render and turbo:render.');
43+
debugTurbolinks('TURBO DETECTED: adding event listeners for turbo:before-render and turbo:render.');
4344
document.addEventListener('turbo:before-render', runPageUnloadedCallbacks);
4445
document.addEventListener('turbo:render', runPageLoadedCallbacks);
4546
runPageLoadedCallbacks();
4647
} else if (turbolinksVersion5()) {
4748
debugTurbolinks(
48-
'USING TURBOLINKS 5: document added event listeners turbolinks:before-render and turbolinks:render.',
49+
'TURBOLINKS 5 DETECTED: adding event listeners for turbolinks:before-render and turbolinks:render.',
4950
);
5051
document.addEventListener('turbolinks:before-render', runPageUnloadedCallbacks);
5152
document.addEventListener('turbolinks:render', runPageLoadedCallbacks);
5253
runPageLoadedCallbacks();
5354
} else {
54-
debugTurbolinks('USING TURBOLINKS 2: document added event listeners page:before-unload and page:change.');
55+
debugTurbolinks('TURBOLINKS 2 DETECTED: adding event listeners for page:before-unload and page:change.');
5556
document.addEventListener('page:before-unload', runPageUnloadedCallbacks);
5657
document.addEventListener('page:change', runPageLoadedCallbacks);
5758
}
5859
}
5960

60-
let isEventListenerInitialized = false;
61+
let isPageLifecycleInitialized = false;
6162
function initializePageEventListeners(): void {
6263
if (typeof window === 'undefined') return;
6364

64-
if (isEventListenerInitialized) {
65+
if (isPageLifecycleInitialized) {
6566
return;
6667
}
67-
isEventListenerInitialized = true;
68+
isPageLifecycleInitialized = true;
6869

6970
if (document.readyState === 'complete') {
70-
setupTurbolinksEventListeners();
71+
setupPageNavigationListeners();
7172
} else {
72-
document.addEventListener('DOMContentLoaded', setupTurbolinksEventListeners);
73+
document.addEventListener('DOMContentLoaded', setupPageNavigationListeners);
7374
}
7475
}
7576

0 commit comments

Comments
 (0)