-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.js
71 lines (64 loc) · 1.91 KB
/
pwa.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const CACHE_VERSION = "1.4.1";
const CACHE_NAME = `kodatos-${CACHE_VERSION}`;
const OFFLINE_URL = "/pwa/offline";
const CACHE_ASSETS = [
`/`,
`/enter-code`,
`${OFFLINE_URL}`,
`/assets/images/404.svg`,
`/assets/images/code.svg`,
`/assets/images/taken.svg`,
`/assets/images/void.svg`,
`/assets/images/sections/0.jpg`,
`/assets/images/sections/1.jpg`,
`/assets/branding/logo.svg`,
`/assets/branding/logo-full.svg`,
`/assets/common.css`,
`/assets/common.proto.css`,
`/assets/common.js`,
`/assets/qrcode.min.js`,
`/assets/qrcode-scan.min.js`
];
self.addEventListener("install", e => {
e.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(CACHE_ASSETS)
.then(() => self.skipWaiting());
})
);
});
self.addEventListener("activate", event => {
event.waitUntil((async () => {
if ("navigationPreload" in self.registration) {
await self.registration.navigationPreload.enable();
}
})());
self.clients.claim();
});
self.addEventListener("fetch", event => {
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(
event.request,
{ ignoreSearch: true }
);
if (cachedResponse) {
return cachedResponse;
}
try {
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (e) {
if (event.request.mode === "navigate") {
const offlineResponse = await cache.match(OFFLINE_URL);
return offlineResponse;
}
return e;
}
})());
});