-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
36 lines (33 loc) · 992 Bytes
/
Copy pathsw.js
File metadata and controls
36 lines (33 loc) · 992 Bytes
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
29
30
31
32
33
34
35
36
// Simple offline-first service worker for GitHub Pages
const CACHE = 'raffle-cache-v2';
const ASSETS = [
'./',
'./index.html',
'./manifest.webmanifest',
'./sw.js',
'./icons/icon-192.png',
'./icons/icon-512.png',
'./icons/apple-touch-icon.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(ASSETS)));
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k))))
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const req = event.request;
event.respondWith(
caches.match(req).then(cached => {
const fetchPromise = fetch(req).then(networkRes => {
caches.open(CACHE).then(cache => cache.put(req, networkRes.clone()));
return networkRes;
}).catch(() => cached);
return cached || fetchPromise;
})
);
});