homepage/content/toys/facelight/sw.js

47 lines
978 B
JavaScript
Raw Permalink Normal View History

2021-03-24 09:52:36 -06:00
// jshint asi:true
2021-03-24 10:09:30 -06:00
var cacheName = "v1";
2021-03-24 09:52:36 -06:00
var content = [
"index.html",
"app.js",
2021-03-24 10:09:30 -06:00
"app.css",
"app.svg",
2021-03-24 09:52:36 -06:00
]
self.addEventListener("install", preCache)
self.addEventListener("fetch", cachingFetch)
self.addEventListener("activate", handleActivate)
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)
}
}
}
2021-03-24 10:09:30 -06:00
function handleActivate(event){
event.waitUntil(cleanup())
}
2021-03-24 09:52:36 -06:00
function preCache(event) {
event.waitUntil(
caches.open(cacheName)
.then(cache => cache.addAll(content))
)
}
// Go try to pull a newer version from the network,
// but return what's in the cache for this request
2021-03-24 10:09:30 -06:00
async function cachingFetch(event) {
let resp = await fetch(event.request)
let cache = await caches.open(cacheName)
cache.put(event.request, resp.clone())
2021-03-24 09:52:36 -06:00
event.respondWith(caches.match(event.request))
}