68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
const CACHE_VERSION = '1.5.0';
|
|
const CACHE_NAME = `quad4-linking-tool-${CACHE_VERSION}`;
|
|
const urlsToCache = ['/', '/favicon.svg', '/manifest.json'];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.addAll(urlsToCache).catch(() => {
|
|
console.log('Some resources failed to cache');
|
|
});
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames.map((cacheName) => {
|
|
if (cacheName !== CACHE_NAME && cacheName.startsWith('quad4-linking-tool-')) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('message', (event) => {
|
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
if (event.request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
if (!event.request.url.startsWith(self.location.origin)) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => {
|
|
if (response) {
|
|
return response;
|
|
}
|
|
return fetch(event.request)
|
|
.then((response) => {
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
const responseToCache = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
return response;
|
|
})
|
|
.catch(() => {
|
|
return caches.match('/') || new Response('Offline', { status: 503 });
|
|
});
|
|
})
|
|
);
|
|
});
|