-
-
Notifications
You must be signed in to change notification settings - Fork 32
SPA Navigation Guide
Michael Green edited this page Nov 11, 2025
·
1 revision
This implementation enhances the Gaseous Games application with proper Single Page Application (SPA) behavior when navigating between the home and library pages, including proper page unloading and cleanup mechanisms.
- Seamless transitions between home and library pages without full page reloads
- Page unloading - old page content is properly cleaned up before loading new content
- Browser navigation support - back/forward buttons work correctly
- URL state management - URLs update correctly and support bookmarking
- Automated cleanup - intervals, timeouts, and event listeners are properly removed
- Memory management - prevents memory leaks from accumulated JavaScript objects
- Custom cleanup callbacks - pages can register their own cleanup functions
- Graceful degradation - if SPA navigation fails, falls back to standard navigation
- Mixed navigation - non-SPA pages (settings, collections, etc.) still use traditional navigation
Enhanced to support proper page unloading:
- Calls
unloadCurrentPage()for home/library pages before loading new content - Tracks current page state
- Maintains existing functionality for other pages
Handles cleanup when switching between SPA pages:
- Calls page-specific unload callbacks
- Performs general cleanup (timers, event listeners, etc.)
- Clears jQuery lazy loading and select2 instances
Smart navigation function:
- Uses SPA navigation for home ↔ library transitions
- Updates browser history with
pushState - Falls back to standard navigation for other pages
Allows pages to register custom cleanup functions:
registerPageUnloadCallback('library', async () => {
// Custom cleanup code here
});- Clears scroll and resize timers
- Saves scroll position to localStorage
- Cleans up filter object callbacks
- Resets loaded pages array
- Removes event listeners
- Clears loading intervals for game rows
- Resets cover URL lists
- Cleans up any ongoing async operations
Navigation buttons now use the enhanced navigateToPage() function with fallback to traditional navigation.
- popstate event listener handles browser back/forward navigation
- History state management preserves page state in browser history
- URL synchronization keeps URL in sync with current page
// Navigate to library page (SPA mode)
navigateToPage('library');
// Navigate to home page (SPA mode)
navigateToPage('home');
// Navigate to settings (traditional mode)
navigateToPage('settings');// Register cleanup for a new page
registerPageUnloadCallback('mypage', async () => {
// Clear timers
if (myPageTimer) {
clearTimeout(myPageTimer);
}
// Remove event listeners
window.removeEventListener('scroll', myScrollHandler);
// Clear data
myPageData = [];
console.log('MyPage cleanup completed');
});// Check if SPA navigation is available
if (typeof navigateToPage === 'function') {
navigateToPage('library');
} else {
// Fallback to traditional navigation
window.location.href = '/index.html?page=library';
}- Modern browsers: Full SPA functionality with history management
- Older browsers: Graceful fallback to traditional navigation
- JavaScript disabled: Standard page navigation still works
- Faster navigation - No full page reloads between home/library
- Reduced server load - Fewer complete page requests
- Better user experience - Smooth transitions without white screens
- Memory efficiency - Proper cleanup prevents memory leaks
- Preserved state - Background images and other elements persist
- Navigate between Home and Library using the navigation buttons
- Use browser back/forward buttons
- Reload the page on home or library and verify state restoration
- Check browser console for cleanup messages
- Test navigation to other pages (settings, collections) to ensure they still work
Look for these console messages indicating proper operation:
"SPA navigation from library to home""Unloading page: library""Library page cleanup completed""Browser navigation to: home"
- Page transitions - Add smooth animations between pages
- Preloading - Preload next likely page for even faster navigation
- State preservation - Maintain scroll position and form state
- Loading indicators - Show progress during page transitions
- Cache management - Cache page content for offline navigation
-
Cleanup not working: Ensure
registerPageUnloadCallbackis called after the function is defined -
History issues: Check that
pushStateis supported in the browser - Memory leaks: Verify all timers and event listeners are properly cleared in cleanup callbacks
Enable verbose logging by adding this to browser console:
// Enable debug logging
localStorage.setItem('spaDebug', 'true');This implementation provides a solid foundation for SPA behavior while maintaining backwards compatibility and proper resource management.