homepage/toys/starship/sw.js

26 lines
712 B
JavaScript
Raw Normal View History

2018-11-21 13:19:31 -07:00
var cacheName = "starship-v1";
var content = [
"index.html",
"starship.js"
];
self.addEventListener("install", e => {
e.waitUntil(caches.Open(cacheName).then(cache => cache.addAll(content)));
});
// Have mercy, this is a horror show
self.addEventListener("fetch", e => {
e.respondWith(
caches.match(e.request).then(r => {
console.log("[Service Worker] Fetching resource:", e.request.url);
return r || fetch(e.request).then(response => {
return caches.open(cacheName).then(cache => {
console.log("[Service Worker] Caching new resource:", e.request.url);
2018-11-21 13:25:05 -07:00
cache.put(e.request, response.clone());
2018-11-21 13:19:31 -07:00
return response;
});
});
})
);
});