-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
21 lines (19 loc) · 658 Bytes
/
Copy pathsw.js
File metadata and controls
21 lines (19 loc) · 658 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Simple cache-first service worker so the installed PWA runs offline.
const CACHE = 'firered-v1';
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));
self.addEventListener('fetch', (e) => {
if (e.request.method !== 'GET') return;
e.respondWith(
caches.open(CACHE).then(async (cache) => {
const hit = await cache.match(e.request);
const net = fetch(e.request)
.then((res) => {
if (res && res.ok) cache.put(e.request, res.clone());
return res;
})
.catch(() => hit);
return hit || net;
})
);
});