-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
31 lines (29 loc) · 1.05 KB
/
service-worker.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
29
30
31
// Files to cache
const cacheName = 'helloworld-pwa';
const appShellFiles = [
'./helloworld-pwa.html',
'./dbr-big.png',
'./helloworld-pwa.webmanifest',
];
// Installing Service Worker
self.addEventListener('install', (e) => {
console.log('[Service Worker] Install');
e.waitUntil((async () => {
const cache = await caches.open(cacheName);
console.log('[Service Worker] Caching all: app shell and content');
await cache.addAll(appShellFiles);
})());
});
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
const r = await caches.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (r) { return r; }
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
if (e.request.method !== "POST")
cache.put(e.request, response.clone());
return response;
})());
});