67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
const CACHE_NAME = 'notes2-{{ .VERSION }}'
|
|
const CACHED_ASSETS = [
|
|
'/',
|
|
'/notes2',
|
|
|
|
'/css/{{ .VERSION }}/main.css',
|
|
'/css/{{ .VERSION }}/notes2.css',
|
|
|
|
'/js/{{ .VERSION }}/lib/preact/preact.mjs',
|
|
'/js/{{ .VERSION }}/lib/htm/htm.mjs',
|
|
'/js/{{ .VERSION }}/lib/preact/devtools.mjs',
|
|
'/js/{{ .VERSION }}/lib/signals/signals.mjs',
|
|
'/js/{{ .VERSION }}/lib/signals/signals-core.mjs',
|
|
'/js/{{ .VERSION }}/lib/preact/hooks.mjs',
|
|
|
|
'/js/{{ .VERSION }}/api.mjs',
|
|
'/js/{{ .VERSION }}/node_store.mjs',
|
|
'/js/{{ .VERSION }}/notes2.mjs',
|
|
'/js/{{ .VERSION }}/key.mjs',
|
|
'/js/{{ .VERSION }}/crypto.mjs',
|
|
'/js/{{ .VERSION }}/checklist.mjs',
|
|
|
|
'/images/{{ .VERSION }}/leaf.svg',
|
|
'/images/{{ .VERSION }}/collapsed.svg',
|
|
'/images/{{ .VERSION }}/expanded.svg',
|
|
]
|
|
|
|
async function precache() {
|
|
const cache = await caches.open(CACHE_NAME)
|
|
return cache.addAll(CACHED_ASSETS)
|
|
}
|
|
|
|
async function fetchAsset(event) {
|
|
try {
|
|
return await fetch(event.request)
|
|
} catch (e) {
|
|
const cache = await caches.open(CACHE_NAME)
|
|
return cache.match(event.request)
|
|
}
|
|
}
|
|
|
|
async function cleanupCache() {
|
|
const keys = await caches.keys()
|
|
const keysToDelete = keys.map(key => {
|
|
if (key !== CACHE_NAME)
|
|
return caches.delete(key)
|
|
})
|
|
return Promise.all(keysToDelete)
|
|
}
|
|
|
|
self.addEventListener('install', event => {
|
|
console.log('SERVICE WORKER: install')
|
|
self.skipWaiting()
|
|
event.waitUntil(precache())
|
|
})
|
|
|
|
self.addEventListener('activate', event => {
|
|
console.log('SERVICE WORKER: activate')
|
|
self.clients.claim()
|
|
event.waitUntil(cleanupCache())
|
|
})
|
|
|
|
self.addEventListener('fetch', event => {
|
|
//console.log('SERVICE WORKER: fetch')
|
|
event.respondWith(fetchAsset(event))
|
|
})
|