Make sw.js non-cacheable and self-update so the worker fix actually reaches clients
CI/CD / CI · dotnet build (push) Successful in 2m38s
CI/CD / Deploy · hamkadr (push) Successful in 3m30s

The sw.js file was cacheable (text/javascript, not covered by the HTML no-cache rule), so browsers/CDN kept serving the old v1 worker and never picked up the v2 fix. Serve sw.js with no-cache/no-store, call reg.update() on load, and reload once on controllerchange so stale cached pages are dropped immediately.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-28 21:24:02 +03:30
parent f0e0b82375
commit 7acf94695f
2 changed files with 18 additions and 3 deletions
@@ -221,7 +221,16 @@
@* Register the PWA service worker (offline + push notifications). *@
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () { navigator.serviceWorker.register('/sw.js').catch(function () {}); });
window.addEventListener('load', function () {
navigator.serviceWorker.register('/sw.js').then(function (reg) {
reg.update(); // always check for a newer worker so fixes reach clients fast
// When a new worker takes control, reload once so stale cached pages are dropped.
var refreshed = false;
navigator.serviceWorker.addEventListener('controllerchange', function () {
if (refreshed) return; refreshed = true; location.reload();
});
}).catch(function () {});
});
}
</script>