forked from school13grodno/school13grodno.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwabuilder-sw.js
69 lines (69 loc) · 1.82 KB
/
pwabuilder-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
/* Versions service worker */
const _LATEST_VERSION = "room-of-military-glory_v2.0.2";
/* Resource cache */
const _ASSETS = [
"/",
"style.css",
"scripts/script.js",
"images/bg-img_lazy.gif",
"images/bg-img.gif",
"images/camera.svg",
"images/close.svg",
"images/document.svg",
"images/double-arrow.svg",
"images/expand.svg",
"images/load.svg",
"images/open-book.svg",
"images/star.svg",
"images/school-logo.png",
"images/belarus-home-room.svg",
"images/github.svg"
];
/* Install service worker */
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(_LATEST_VERSION).then((cache) => {
/* Add minimum resources */
return cache.addAll(_ASSETS)
}).catch((error) => console.log(error))
);
/* Skip waiting new update service working */
self.skipWaiting();
});
/* Activate service worker */
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((names) => {
for (let name of names) {
if (name !== _LATEST_VERSION) {
/* Delete previous version */
caches.delete(name);
}
}
})
);
/* Use updated service worker */
self.clients.claim();
});
/* Use service worker */
self.addEventListener('fetch', (event) => {
/* Skip cross-origin requests, like those for Google Analytics */
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then((resp) => {
/* Return from cache if available
else add to cache */
return resp || fetch(event.request).then((response) => {
/* Add to cache if response status OK */
if (response.status == '200') {
let _responseClone = response.clone();
caches.open(_LATEST_VERSION).then((cache) => {
cache.put(event.request, _responseClone);
});
}
return response;
});
}).catch((error) => console.log(error))
);
}
});