diff --git a/src/frontend/onLoad.ts b/src/frontend/onLoad.ts index 4e1e225..8b16c25 100644 --- a/src/frontend/onLoad.ts +++ b/src/frontend/onLoad.ts @@ -1,7 +1,7 @@ // Client-side script that runs on page load // Example: TypeScript with type annotations -async function loadContent(url: string) { +async function loadContent(url: string, shouldScrollToTop: boolean = true) { const response = await fetch(url, { headers: { 'shell-loaded': 'true' @@ -13,6 +13,10 @@ async function loadContent(url: string) { mainElement.outerHTML = html; // Re-attach handlers to new links after content swap attachLinkHandlers(); + // Smooth scroll to top after navigation if needed + if (shouldScrollToTop) { + window.scrollTo({ top: 0, behavior: 'smooth' }); + } } } @@ -27,14 +31,14 @@ function attachLinkHandlers() { e.stopPropagation(); e.stopImmediatePropagation(); window.history.pushState({}, '', link.href); - await loadContent(link.href); + await loadContent(link.href, true); } }); } // Listen for back/forward button clicks window.addEventListener('popstate', async (event) => { - await loadContent(window.location.href); + await loadContent(window.location.href, false); }); document.addEventListener('DOMContentLoaded', () => {