FIX: Add listeners to update post-archive more reliably

This commit is contained in:
Caleb Braaten 2026-01-09 00:41:13 -08:00
parent 58fa014341
commit d5450a3c0a

View File

@ -244,4 +244,55 @@ window.addEventListener('popstate', () => {
initializeArchiveState(); initializeArchiveState();
// Update the active post indicator after initializing the archive state // Update the active post indicator after initializing the archive state
setTimeout(updateActivePost, 10); // Small delay to ensure DOM is updated setTimeout(updateActivePost, 10); // Small delay to ensure DOM is updated
}); });
// Listen for URL changes (including navigation via other components)
function setupUrlChangeListener() {
// Store the current URL to detect changes
let currentUrl = window.location.pathname;
// Use MutationObserver to detect DOM changes in the main element
// This helps catch navigation changes from other components
const observer = new MutationObserver((mutations) => {
// Check if URL has changed
if (window.location.pathname !== currentUrl) {
currentUrl = window.location.pathname;
// Wait a bit for any DOM updates to complete
setTimeout(() => {
updateActivePost();
initializeArchiveState();
}, 50);
}
});
// Start observing the main element for changes
const mainElement = document.querySelector('main');
if (mainElement) {
observer.observe(mainElement, { childList: true, subtree: true });
}
// Also observe direct URL changes (e.g., when using browser back/forward)
const intervalId = setInterval(() => {
if (window.location.pathname !== currentUrl) {
currentUrl = window.location.pathname;
updateActivePost();
initializeArchiveState();
}
}, 500); // Check every 500ms
// Clean up on page unload
window.addEventListener('beforeunload', () => {
clearInterval(intervalId);
observer.disconnect();
});
}
// Initialize the URL change listener
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
setupUrlChangeListener();
});
} else {
setupUrlChangeListener();
}