Last active
April 8, 2023 04:47
-
-
Save shinokada/2c21ed07cbbf50e79b867346d1a88940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GHPATH = 'https://vite-svelte-pwa.codewithshin.com'; | |
const APP_PREFIX = 'vite_svelte_pwa_'; | |
const VERSION = 'version_02'; | |
const URLS = [ | |
`${GHPATH}/`, | |
`${GHPATH}/index.html` | |
]; | |
const 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]); | |
} | |
}) | |
); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment