fix: SW uses network-first strategy, updates apply without clearing storage

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Siavash Sameni
2026-03-29 09:11:15 +04:00
parent 0697c988fa
commit deb220ff2c

View File

@@ -50,7 +50,7 @@ async fn pwa_manifest() -> impl IntoResponse {
async fn service_worker() -> impl IntoResponse {
([(header::CONTENT_TYPE, "application/javascript")], r##"
const CACHE = 'wz-v5';
const CACHE = 'wz-v6';
const SHELL = ['/', '/wasm/warzone_wasm.js', '/wasm/warzone_wasm_bg.wasm', '/icon.svg', '/manifest.json'];
self.addEventListener('install', e => {
@@ -71,18 +71,17 @@ self.addEventListener('fetch', e => {
if (url.pathname.startsWith('/v1/')) return;
// WS: skip
if (url.protocol === 'ws:' || url.protocol === 'wss:') return;
// Shell: cache first, network fallback
// Network first, cache fallback (ensures updates are picked up immediately)
e.respondWith(
caches.match(e.request).then(cached => cached || fetch(e.request).then(resp => {
fetch(e.request).then(resp => {
if (resp.ok && SHELL.includes(url.pathname)) {
const clone = resp.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
}
return resp;
}).catch(() => {
if (e.request.mode === 'navigate') {
return caches.match('/');
}
}).catch(() => caches.match(e.request).then(cached => {
if (cached) return cached;
if (e.request.mode === 'navigate') return caches.match('/');
}))
);
});