-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserviceWorker.js
28 lines (25 loc) · 1.06 KB
/
serviceWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const CACHE_NAME = "langton-music-v2";
if (location.protocol.indexOf('file') === -1 && location.host.indexOf('localhost') === -1) {
// Copied from https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate
self.addEventListener('fetch', e => {
e.respondWith(caches.open(CACHE_NAME).then(cache => {
return cache.match(e.request).then(cachedResponse => {
const fetchedResponse = fetch(e.request).then(networkResponse => {
cache.put(e.request, networkResponse.clone());
return networkResponse;
});
return cachedResponse || fetchedResponse;
});
}));
});
self.addEventListener("install", () => {
console.log("service worker installed");
self.skipWaiting();
});
self.addEventListener("activate", e => {
console.log("service worker activated");
e.waitUntil(self.clients.claim());
});
} else {
console.error('Service worker is aborting, development version');
}