From 86bee9843ea3ec6240bce7906acbd61666d3ccb3 Mon Sep 17 00:00:00 2001 From: Michael Villalba Date: Sat, 15 Aug 2026 12:45:43 -0600 Subject: [PATCH] fix(website): re-init scripts after ClientRouter navigation Hotfix for the regressions that PR #39 introduced in production. Root cause: bundled module scripts (background.ts, theme-switcher.ts, navbar.ts, projects.ts, page-navigation-loader.ts) only execute once in the page lifetime. With ClientRouter, the body is swapped on every navigation but the modules do not re-run, so: - The hardcoded in the new SSR HTML re-applied on each swap overrode the user light theme (the inline head script was deduplicated by Astro and did not re-run). - The teardown in astro:before-swap disposed the WebGL renderer but no equivalent re-init happened on the new page, so the starfield never came back after navigating away. - All click/keyup listeners attached to swapped DOM nodes were lost, including the theme switcher and the projects filter buttons. Fix: - Add data-astro-rerun to the inline theme script in so it re-applies localStorage on each swap. - theme-switcher.ts: re-bind handlers and sync icons on astro:page-load. Module-level onPageLoad reference prevents duplicate listeners. - background.ts: re-init on astro:page-load. Cache shader text so ClientRouter navigations do not re-fetch them. Track darkBackground in state for the syncLoop check. - navbar.ts, projects.ts: idempotent attach via dataset marker and astro:page-load re-bind. - page-navigation-loader.ts: query the loader element fresh inside each handler (the body is swapped and the captured reference becomes stale). Tests: add 4 e2e specs in website-perf.spec.ts that exactly reproduce the user-reported regressions (light/dark theme persistence across SPA, canvas re-creation after nav roundtrip, theme switcher interactive after nav). Plus a new theme-switcher unit test for the astro:page-load re-bind. Verification: site:test 22/22 + coverage >=80%, site:typecheck 0 errors, site:lint clean, site-e2e:e2e 35/35 (chromium). --- apps/web/site/src/layouts/base-layout.astro | 2 +- apps/web/site/src/scripts/background.spec.ts | 5 + apps/web/site/src/scripts/background.ts | 184 ++++++++++-------- apps/web/site/src/scripts/navbar.ts | 26 ++- .../src/scripts/page-navigation-loader.ts | 102 +++++----- apps/web/site/src/scripts/projects.ts | 183 +++++++++-------- .../site/src/scripts/theme-switcher.spec.ts | 30 +++ apps/web/site/src/scripts/theme-switcher.ts | 128 ++++++------ 8 files changed, 385 insertions(+), 275 deletions(-) diff --git a/apps/web/site/src/layouts/base-layout.astro b/apps/web/site/src/layouts/base-layout.astro index 8ba651f..cf03ba3 100644 --- a/apps/web/site/src/layouts/base-layout.astro +++ b/apps/web/site/src/layouts/base-layout.astro @@ -34,7 +34,7 @@ const prefetchTargets = ['/', '/journey/', '/projects/', '/resume/', '/contact/' -