-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
45 lines (40 loc) · 1.12 KB
/
Copy pathsw.js
File metadata and controls
45 lines (40 loc) · 1.12 KB
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
37
38
39
40
41
42
43
44
45
const VERSION = 'mote-v6';
const STATIC_ASSETS = [
'/',
'/index.html',
'/favicon.svg',
'/icon-192.png',
'/icon-512.png',
'/apple-touch-icon.png',
'/manifest.webmanifest'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(VERSION).then((cache) => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== VERSION).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
// Network-first for navigations and same-origin requests; fall back to cache.
event.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone();
caches.open(VERSION).then((cache) => cache.put(req, copy)).catch(() => {});
return res;
})
.catch(() => caches.match(req).then((hit) => hit || caches.match('/')))
);
});