37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
const CACHE_NAME = 'secret-message-studio-v3';
|
|
const ASSETS = [
|
|
'./',
|
|
'./index.html',
|
|
'./manifest.json',
|
|
'./icon.svg',
|
|
'https://cdn.jsdelivr.net/npm/qrcode@1.5.4/build/qrcode.min.js',
|
|
'https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.min.js'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)).catch(() => undefined)
|
|
);
|
|
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) => {
|
|
if (event.request.method !== 'GET') return;
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => cached || fetch(event.request).then((response) => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, copy)).catch(() => undefined);
|
|
return response;
|
|
}).catch(() => caches.match('./index.html')))
|
|
);
|
|
});
|