|
|
|
const CACHE_NAME = 'forum-communautaire-v1'; |
|
const ASSETS_TO_CACHE = [ |
|
'/', |
|
'/static/css/styles.css', |
|
'/static/js/forum.js', |
|
'/static/js/editor.js', |
|
'/static/icons/icon-192x192.png', |
|
'/static/icons/icon-512x512.png', |
|
'https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js', |
|
'https://cdn.tailwindcss.com' |
|
]; |
|
|
|
|
|
self.addEventListener('install', (event) => { |
|
event.waitUntil( |
|
caches.open(CACHE_NAME) |
|
.then((cache) => { |
|
console.log('Cache ouvert'); |
|
return cache.addAll(ASSETS_TO_CACHE); |
|
}) |
|
); |
|
}); |
|
|
|
|
|
self.addEventListener('activate', (event) => { |
|
const cacheWhitelist = [CACHE_NAME]; |
|
event.waitUntil( |
|
caches.keys().then((cacheNames) => { |
|
return Promise.all( |
|
cacheNames.map((cacheName) => { |
|
if (cacheWhitelist.indexOf(cacheName) === -1) { |
|
return caches.delete(cacheName); |
|
} |
|
}) |
|
); |
|
}) |
|
); |
|
}); |
|
|
|
|
|
self.addEventListener('fetch', (event) => { |
|
event.respondWith( |
|
caches.match(event.request) |
|
.then((response) => { |
|
|
|
if (response) { |
|
return response; |
|
} |
|
|
|
|
|
const fetchRequest = event.request.clone(); |
|
|
|
return fetch(fetchRequest).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; |
|
} |
|
); |
|
}) |
|
); |
|
}); |