fix: change SW cache mechanism

This commit is contained in:
simosmik
2026-03-21 16:49:56 +00:00
parent a41d2c713e
commit 17d6ec54af

View File

@@ -1,8 +1,8 @@
// Service Worker for Claude Code UI PWA
const CACHE_NAME = 'claude-ui-v1';
// Cache only manifest (needed for PWA install). HTML and JS are never pre-cached
// so a rebuild + refresh always picks up the latest assets.
const CACHE_NAME = 'claude-ui-v2';
const urlsToCache = [
'/',
'/index.html',
'/manifest.json'
];
@@ -10,44 +10,63 @@ const urlsToCache = [
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(urlsToCache);
})
.then(cache => cache.addAll(urlsToCache))
);
self.skipWaiting();
});
// Fetch event
// Fetch event — network-first for everything except hashed assets
self.addEventListener('fetch', event => {
// Never cache API requests or WebSocket upgrades
if (event.request.url.includes('/api/') || event.request.url.includes('/ws')) {
const url = event.request.url;
// Never intercept API requests or WebSocket upgrades
if (url.includes('/api/') || url.includes('/ws')) {
return;
}
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
// Navigation requests (HTML) — always go to network, no caching
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).catch(() => caches.match('/manifest.json').then(() =>
new Response('<h1>Offline</h1><p>Please check your connection.</p>', {
headers: { 'Content-Type': 'text/html' }
})
))
);
return;
}
// Hashed assets (JS/CSS in /assets/) — cache-first since filenames change per build
if (url.includes('/assets/')) {
event.respondWith(
caches.match(event.request).then(cached => {
if (cached) return cached;
return fetch(event.request).then(response => {
const clone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
return response;
}
return fetch(event.request);
}
)
});
})
);
return;
}
// Everything else — network-first
event.respondWith(
fetch(event.request).catch(() => caches.match(event.request))
);
});
// Activate event
// Activate event — purge old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
caches.keys().then(cacheNames =>
Promise.all(
cacheNames
.filter(name => name !== CACHE_NAME)
.map(name => caches.delete(name))
)
)
);
self.clients.claim();
});