From b5d5ed1d546b5bf30e018d79ec338fd286cc2f29 Mon Sep 17 00:00:00 2001 From: kaheetonaa <61049296+kaheetonaa@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:54:20 +0200 Subject: [PATCH] update sw.js --- sw.js | 71 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/sw.js b/sw.js index 79d9a8e..db4c212 100644 --- a/sw.js +++ b/sw.js @@ -1,19 +1,52 @@ -self.addEventListener('install', (e) => { - e.waitUntil( - caches.open('iDSandbox4All').then((cache) => cache.addAll([ - '/id_sandbox_for_all/', - '/id_sandbox_for_all/index.html', - '/id_sandbox_for_all/land.html', - '/id_sandbox_for_all/icon/icon.png', - '/id_sandbox_for_all/dist/iD.js', - '/id_sandbox_for_all/dist/iD.css', - ])), - ); - }); - - self.addEventListener('fetch', (e) => { - console.log(e.request.url); - e.respondWith( - caches.match(e.request).then((response) => response || fetch(e.request)), - ); - }); \ No newline at end of file +var GHPATH = '/github-page-pwa'; +var APP_PREFIX = 'gppwa_'; +var VERSION = 'version_002'; +var URLS = [ + `${GHPATH}/`, + `${GHPATH}/index.html`, + `${GHPATH}/css/styles.css`, + `${GHPATH}/img/icon.png`, + `${GHPATH}/js/app.js` +] + +var CACHE_NAME = APP_PREFIX + VERSION +self.addEventListener('fetch', function (e) { + console.log('Fetch request : ' + e.request.url); + e.respondWith( + caches.match(e.request).then(function (request) { + if (request) { + console.log('Responding with cache : ' + e.request.url); + return request + } else { + console.log('File is not cached, fetching : ' + e.request.url); + return fetch(e.request) + } + }) + ) +}) + +self.addEventListener('install', function (e) { + e.waitUntil( + caches.open(CACHE_NAME).then(function (cache) { + console.log('Installing cache : ' + CACHE_NAME); + return cache.addAll(URLS) + }) + ) +}) + +self.addEventListener('activate', function (e) { + e.waitUntil( + caches.keys().then(function (keyList) { + var cacheWhitelist = keyList.filter(function (key) { + return key.indexOf(APP_PREFIX) + }) + cacheWhitelist.push(CACHE_NAME); + return Promise.all(keyList.map(function (key, i) { + if (cacheWhitelist.indexOf(key) === -1) { + console.log('Deleting cache : ' + keyList[i] ); + return caches.delete(keyList[i]) + } + })) + }) + ) +}) \ No newline at end of file