44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'whatsapp-static-v1';
|
|
const OFFLINE_URL = '/index.php';
|
|
const ASSETS_TO_CACHE = [
|
|
OFFLINE_URL,
|
|
'/assets/css/styles.css',
|
|
'/assets/js/app_simple.js',
|
|
'/manifest.json'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS_TO_CACHE))
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys => Promise.all(
|
|
keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))
|
|
))
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
if (event.request.method !== 'GET') return;
|
|
event.respondWith(
|
|
caches.match(event.request).then(cached => {
|
|
if (cached) return cached;
|
|
return fetch(event.request).then(resp => {
|
|
// cache same-origin GET responses
|
|
try {
|
|
if (resp && resp.status === 200 && resp.type === 'basic') {
|
|
const clone = resp.clone();
|
|
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
|
|
}
|
|
} catch (e) {}
|
|
return resp;
|
|
}).catch(() => caches.match(OFFLINE_URL));
|
|
})
|
|
);
|
|
});
|