54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
const CACHE_NAME = 'hxops-shell-v2';
|
|
const OFFLINE_ASSETS = ['/', '/workspace', '/manifest.webmanifest', '/vite.svg'];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(OFFLINE_ASSETS)).catch(() => null)
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(
|
|
keys
|
|
.filter((key) => key !== CACHE_NAME)
|
|
.map((key) => caches.delete(key))
|
|
)
|
|
)
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const request = event.request;
|
|
if (request.method !== 'GET') return;
|
|
|
|
const url = new URL(request.url);
|
|
if (url.origin !== self.location.origin) return;
|
|
if (url.pathname.startsWith('/api/')) return;
|
|
if (url.pathname.startsWith('/@vite/')) return;
|
|
if (url.pathname.includes('/__vite_ping')) return;
|
|
|
|
if (request.mode === 'navigate') {
|
|
event.respondWith(
|
|
fetch(request).catch(() => caches.match('/workspace').then((res) => res || caches.match('/')))
|
|
);
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(request).then((cached) => {
|
|
if (cached) return cached;
|
|
return fetch(request)
|
|
.then((response) => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy)).catch(() => null);
|
|
return response;
|
|
})
|
|
.catch(() => caches.match('/vite.svg'));
|
|
})
|
|
);
|
|
});
|