diff --git a/service-worker.js b/service-worker.js index aa5e9be..3441deb 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,11 +1,16 @@ +const CACHE_NAME = 'instinct-app-cache'; +const urlsToCache = [ + './', + './index.html', + './manifest.json', + './icon-192x192.png', + './icon-512x512.png', +]; + self.addEventListener('install', (event) => { event.waitUntil( - caches.open('instinct-app-cache').then((cache) => { - return cache.addAll([ - '/', - 'index.html', - 'manifest.json', - ]); + caches.open(CACHE_NAME).then((cache) => { + return cache.addAll(urlsToCache); }) ); }); @@ -17,3 +22,18 @@ self.addEventListener('fetch', (event) => { }) ); }); + +self.addEventListener('activate', (event) => { + const cacheWhitelist = [CACHE_NAME]; + event.waitUntil( + caches.keys().then((keyList) => { + return Promise.all( + keyList.map((key) => { + if (!cacheWhitelist.includes(key)) { + return caches.delete(key); + } + }) + ); + }) + ); +});