Fixed + cleaned service worker

This commit is contained in:
Neale Pickett 2021-03-24 10:09:30 -06:00
parent e9c4f78d98
commit c669e6f812
1 changed files with 12 additions and 16 deletions

View File

@ -1,10 +1,11 @@
// jshint asi:true // jshint asi:true
var cacheName = "v2"; var cacheName = "v1";
var content = [ var content = [
"index.html", "index.html",
"app.js", "app.js",
"app.png", "app.css",
"app.svg",
] ]
@ -12,12 +13,6 @@ self.addEventListener("install", preCache)
self.addEventListener("fetch", cachingFetch) self.addEventListener("fetch", cachingFetch)
self.addEventListener("activate", handleActivate) self.addEventListener("activate", handleActivate)
function handleActivate(event){
event.waitUntil(
cleanup()
)
}
async function cleanup(event) { async function cleanup(event) {
let cacheNames = await caches.keys() let cacheNames = await caches.keys()
for (let name of cacheNames) { for (let name of cacheNames) {
@ -28,6 +23,11 @@ async function cleanup(event) {
} }
} }
function handleActivate(event){
event.waitUntil(cleanup())
}
function preCache(event) { function preCache(event) {
event.waitUntil( event.waitUntil(
caches.open(cacheName) caches.open(cacheName)
@ -37,14 +37,10 @@ function preCache(event) {
// Go try to pull a newer version from the network, // Go try to pull a newer version from the network,
// but return what's in the cache for this request // but return what's in the cache for this request
function cachingFetch(event) { async function cachingFetch(event) {
fetch(event.request) let resp = await fetch(event.request)
.then(resp => { let cache = await caches.open(cacheName)
caches.open(cacheName) cache.put(event.request, resp.clone())
.then(cache => {
cache.put(event.request, resp.clone())
})
})
event.respondWith(caches.match(event.request)) event.respondWith(caches.match(event.request))
} }