Skip to content

fix(website): re-init scripts after ClientRouter navigation - #40

Merged
visomi-dev merged 1 commit into
mainfrom
perf/website-scroll-and-nav-performance
Aug 15, 2026
Merged

visomi-dev merged 1 commit into
mainfrom
perf/website-scroll-and-nav-performance

Conversation

@visomi-dev

Copy link
Copy Markdown
Owner

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).

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 <html class="dark"> 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 <head> 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).
@visomi-dev
visomi-dev merged commit 9511109 into main Aug 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant