fix(sw): skip controllerchange reload on first activation

The reload should only fire when an existing SW is replaced by a new one
(old → new), not when the SW activates for the first time on a fresh page
load (null → first). The spurious reload on fresh loads was caused by
checking unconditionally.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Idem
2026-06-22 15:26:48 -04:00
parent 677ec9d918
commit cf9975f50f

View File

@@ -344,7 +344,12 @@ $effect(() => {
// silently runs whatever JS it had at the time the SW changed. This caused IDAA and // silently runs whatever JS it had at the time the SW changed. This caused IDAA and
// other users to run stale code for weeks at a time across multiple deployments. // other users to run stale code for weeks at a time across multiple deployments.
// Added 2026-06-22 to close this gap. // Added 2026-06-22 to close this gap.
const on_controller_change = () => window.location.reload(); // Capture before registering — if null, this is first activation (fresh page load)
// and the reload is unnecessary since assets were already fetched without the SW.
const had_controller = !!navigator.serviceWorker.controller;
const on_controller_change = () => {
if (had_controller) window.location.reload();
};
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.addEventListener('controllerchange', on_controller_change); navigator.serviceWorker.addEventListener('controllerchange', on_controller_change);
} }