2020-03-08 12:03:22 -06:00
|
|
|
// jshint asi:true
|
|
|
|
|
|
|
|
var cacheName = "v2";
|
2020-03-07 20:22:31 -07:00
|
|
|
var content = [
|
|
|
|
"index.html",
|
|
|
|
"convulse.css",
|
|
|
|
"convulse.js",
|
|
|
|
"convulse.png"
|
2020-03-08 12:03:22 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener("install", preCache)
|
|
|
|
self.addEventListener("fetch", cachingFetch)
|
|
|
|
self.addEventListener("activate", handleActivate)
|
|
|
|
|
|
|
|
function handleActivate(event){
|
|
|
|
event.waitUntil(
|
|
|
|
cleanup()
|
|
|
|
)
|
|
|
|
}
|
2020-03-07 20:22:31 -07:00
|
|
|
|
2020-03-08 12:03:22 -06:00
|
|
|
async function cleanup(event) {
|
|
|
|
let cacheNames = await caches.keys()
|
|
|
|
for (let name of cacheNames) {
|
|
|
|
if (name != cacheName) {
|
|
|
|
console.log("Deleting old cache", name)
|
|
|
|
caches.delete(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 20:22:31 -07:00
|
|
|
|
2020-03-08 12:03:22 -06:00
|
|
|
function preCache(event) {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.open(cacheName)
|
|
|
|
.then(cache => cache.addAll(content))
|
|
|
|
)
|
|
|
|
}
|
2020-03-07 20:22:31 -07:00
|
|
|
|
2020-03-08 12:03:22 -06:00
|
|
|
// Go try to pull a newer version from the network,
|
|
|
|
// but return what's in the cache for this request
|
|
|
|
function cachingFetch(event) {
|
|
|
|
fetch(event.request)
|
|
|
|
.then(resp => {
|
|
|
|
caches.open(cacheName)
|
|
|
|
.then(cache => {
|
|
|
|
cache.put(event.request, resp.clone())
|
2020-03-07 20:22:31 -07:00
|
|
|
})
|
2020-03-08 12:03:22 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
event.respondWith(caches.match(event.request))
|
|
|
|
}
|