mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-02 10:35:37 +08:00
fix: change SW cache mechanism
This commit is contained in:
73
public/sw.js
73
public/sw.js
@@ -1,8 +1,8 @@
|
|||||||
// Service Worker for Claude Code UI PWA
|
// 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 = [
|
const urlsToCache = [
|
||||||
'/',
|
|
||||||
'/index.html',
|
|
||||||
'/manifest.json'
|
'/manifest.json'
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -10,44 +10,63 @@ const urlsToCache = [
|
|||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', event => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME)
|
caches.open(CACHE_NAME)
|
||||||
.then(cache => {
|
.then(cache => cache.addAll(urlsToCache))
|
||||||
return cache.addAll(urlsToCache);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch event
|
// Fetch event — network-first for everything except hashed assets
|
||||||
self.addEventListener('fetch', event => {
|
self.addEventListener('fetch', event => {
|
||||||
// Never cache API requests or WebSocket upgrades
|
const url = event.request.url;
|
||||||
if (event.request.url.includes('/api/') || event.request.url.includes('/ws')) {
|
|
||||||
|
// Never intercept API requests or WebSocket upgrades
|
||||||
|
if (url.includes('/api/') || url.includes('/ws')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
event.respondWith(
|
// Navigation requests (HTML) — always go to network, no caching
|
||||||
caches.match(event.request)
|
if (event.request.mode === 'navigate') {
|
||||||
.then(response => {
|
event.respondWith(
|
||||||
if (response) {
|
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 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 => {
|
self.addEventListener('activate', event => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then(cacheNames => {
|
caches.keys().then(cacheNames =>
|
||||||
return Promise.all(
|
Promise.all(
|
||||||
cacheNames.map(cacheName => {
|
cacheNames
|
||||||
if (cacheName !== CACHE_NAME) {
|
.filter(name => name !== CACHE_NAME)
|
||||||
return caches.delete(cacheName);
|
.map(name => caches.delete(name))
|
||||||
}
|
)
|
||||||
})
|
)
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
self.clients.claim();
|
self.clients.claim();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user