41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
|
|
const CACHE_NAME = 'version-1';
|
|
const urlsToCache = [ '/', '/index.php', '/assets/css/custom.css', '/assets/js/main.js' ];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
if (response) {
|
|
return response;
|
|
}
|
|
return fetch(event.request);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
const cacheWhitelist = [];
|
|
cacheWhitelist.push(CACHE_NAME);
|
|
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if(!cacheWhitelist.includes(cacheName)) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
))
|
|
)
|
|
});
|