-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
74 lines (71 loc) · 2.31 KB
/
service-worker.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
var CACHE_NAME = "education-v1";
var urlsToCache = [
"index.html",
"/src/assets/global.css",
"/src/assets/bootstrap.min.css",
"/src/assets/bootstrap-icons/font/bootstrap-icons.min.css",
"/src/main.js",
"/src/assets/bootstrap.bundle.min.js",
"images/icon-192.png",
"images/icon-512.png",
"assets/img/science.png",
"assets/img/history.png",
"assets/img/programming.png",
"assets/img/geography.png",
"assets/img/chemistry.png",
"assets/img/computer.png",
"assets/img/marketing.png",
"assets/img/psychology.png",
"assets/img/language.png",
"assets/img/art.png",
"assets/img/economics.png",
"assets/img/music.png",
"assets/img/environment.png",
"assets/img/philosophy.png",
"assets/img/sociology.png",
"assets/img/business.png",
"assets/img/health.png",
"assets/img/astronomy.png",
"assets/img/literature.png"
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
if (response) {
return response;
}
return fetch(event.request).then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
var responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
});
})
);
});
self.addEventListener('activate', (event) => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then((cacheNames) => Promise.all(
cacheNames.map((cacheName) => {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
))
);
});