-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
82 lines (74 loc) · 2.22 KB
/
sw.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
72
73
74
75
76
77
78
79
80
81
82
// Cache name has a timestamp because the browser re-caches the assets when the service worker file is modified
const staticCacheName = "nonogram-cache-" + "22-10-04-0000";
const assets = [
'/nonogram/',
'/nonogram/nonogram.css',
'/nonogram/nonogram.js',
'/nonogram/lib/Grid',
'/nonogram/lib/Rng',
'/repo/PinchToZoom',
'/assets/ext/bootstrap.5.1.3.min.css',
];
const assets_extra = [
'/nonogram/images/cross.svg',
'/nonogram/images/cross-white.svg',
'/nonogram/images/refresh.svg',
'/nonogram/images/transparent.webp',
'/nonogram/images/trash.svg',
'/nonogram/images/lock.svg',
'/nonogram/images/unlock.svg',
'/nonogram/fonts/font-files/MPLUSRounded1c-Regular.20-3f.woff2',
'/nonogram/fonts/font-files/MPLUSRounded1c-Regular.40-7f.woff2',
'/nonogram/fonts/font-files/MPLUSRounded1c-Medium.20-3f.woff2',
'/nonogram/fonts/font-files/MPLUSRounded1c-Medium.40-7f.woff2',
'/nonogram/sounds/pop-39222.mp3',
'/nonogram/sounds/notification-sound-7062.mp3',
'/nonogram/sounds/stop-13692.mp3',
];
self.addEventListener('install', (evt) => {
evt.waitUntil(
(async () => {
const cache = await caches.open(staticCacheName);
cache.addAll(assets_extra).then();
await cache.addAll(assets);
})()
);
});
self.addEventListener('activate', event => {
event.waitUntil(
(async () => {
let cacheNames = await caches.keys();
await Promise.all(cacheNames.map((cacheName) => {
if (staticCacheName.indexOf(cacheName) === -1) return caches.delete(cacheName);
}));
})()
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
(async () => {
// try cache
let response = await caches.match(event.request.url, {
cacheName: staticCacheName,
ignoreSearch: false
});
if (!!response) return response;
// next try internet
try {
response = await fetch(event.request);
} catch (e) {
// if there's no internet, we'll ignore the query string
return await caches.match(event.request.url, {
cacheName: staticCacheName,
ignoreSearch: true
});
}
// add new file to cache (asynchronously)
if (response.ok) {
let clone = response.clone();
caches.open(staticCacheName).then((cache) => cache.put(event.request, clone));
}
return response;
})()
);
});