fix(gateway-ui): refresh service worker and static cache headers
This commit is contained in:
+30
-9
@@ -1,4 +1,4 @@
|
||||
const CACHE_NAME = 'flynn-webchat-v1';
|
||||
const CACHE_NAME = 'flynn-webchat-v2';
|
||||
const token = new URL(self.location.href).searchParams.get('token');
|
||||
const withToken = (path) => {
|
||||
if (!token) {
|
||||
@@ -34,17 +34,38 @@ self.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith((async () => {
|
||||
const cached = await caches.match(event.request, { ignoreSearch: true });
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const requestUrl = new URL(event.request.url);
|
||||
const isNavigation = event.request.mode === 'navigate';
|
||||
const isStaticAsset = requestUrl.origin === self.location.origin
|
||||
&& (requestUrl.pathname.startsWith('/lib/')
|
||||
|| requestUrl.pathname.startsWith('/pages/')
|
||||
|| requestUrl.pathname === '/app.js'
|
||||
|| requestUrl.pathname === '/style.css'
|
||||
|| requestUrl.pathname === '/index.html'
|
||||
|| requestUrl.pathname === '/manifest.webmanifest');
|
||||
|
||||
event.respondWith((async () => {
|
||||
try {
|
||||
return await fetch(event.request);
|
||||
const networkResponse = await fetch(event.request);
|
||||
if (isNavigation || isStaticAsset) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
await cache.put(event.request, networkResponse.clone());
|
||||
}
|
||||
return networkResponse;
|
||||
} catch {
|
||||
const fallback = await caches.match('/index.html', { ignoreSearch: true });
|
||||
return fallback || new Response('Offline', { status: 503 });
|
||||
const cached = await caches.match(event.request, { ignoreSearch: true });
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
if (isNavigation) {
|
||||
const fallback = await caches.match('/index.html', { ignoreSearch: true });
|
||||
if (fallback) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Offline', { status: 503 });
|
||||
}
|
||||
})());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user