Make sw.js non-cacheable and self-update so the worker fix actually reaches clients
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:
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user