aboutsummaryrefslogtreecommitdiffstats
path: root/src/assets/sw.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/assets/sw.mjs')
-rw-r--r--src/assets/sw.mjs51
1 files changed, 19 insertions, 32 deletions
diff --git a/src/assets/sw.mjs b/src/assets/sw.mjs
index f981da5..e20102a 100644
--- a/src/assets/sw.mjs
+++ b/src/assets/sw.mjs
@@ -1,38 +1,34 @@
-const cache_name = "v3"
+const get_cache_info = async() => {
+ const response = await fetch('/cache.json')
+ return response.json()
+}
-const install_sw = async event => {
+self.addEventListener('install', async event => {
const precache = async() => {
- const cache = await self.caches.open(cache_name);
- return cache.addAll([
- '/index.css',
- '/prism.css',
- '/index.js',
- '/sw.js',
- '/favicon.svg',
- '/offline.html',
- '/'
- ])
+ const ver = await get_cache_info()
+ const cache = await self.caches.open(ver.store);
+ return cache.addAll(ver.default)
}
event.waitUntil(precache())
-}
-
-const activate_sw = async event => {
- const cachepreserve = ['v3'];
+})
+self.addEventListener('activate', async event => {
const invalidatecache = async() => {
+ const ver = await get_cache_info()
const keys = await self.caches.keys()
Promise.all(
- keys.map((key) => {
- if (cachepreserve.indexOf(key) === -1) {
+ keys.map(key => {
+ if (key !== ver.store) {
return self.caches.delete(key)
}
}))
}
event.waitUntil(invalidatecache())
-}
+})
-const fetch_sw = async event => {
+self.addEventListener('fetch', async event => {
const cache_fetch = async() => {
+ const ver = await get_cache_info()
if (event.request.method != 'GET') {
return
}
@@ -42,22 +38,13 @@ const fetch_sw = async event => {
}
try {
const response = await self.fetch(event.request)
- const cache = await self.caches.open(cache_name)
+ const cache = await self.caches.open(ver.store)
cache.put(event.request, response.clone())
return response
} catch {
- return self.caches.match('/offline.html')
+ return self.caches.match(ver.offline)
}
}
event.respondWith(cache_fetch())
-}
-
-const main = async() => {
- self.addEventListener('install', install_sw)
- self.addEventListener('activate', activate_sw)
- self.addEventListener('fetch', fetch_sw)
-
-}
-
-main() \ No newline at end of file
+}) \ No newline at end of file