This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
66 lines (62 loc) · 1.69 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
// service-worker.js
const CACHE_NAME = 'my-cache';
const urlsToCache = [
'/',
'/index.html',
'/matiere.html',
'/st.html',
'/info.html',
'/learn',
'/learn/index.html',
'/style.css',
'/js/main.js',
'/js/script.js',
'/js/modal.js',
'/learn/library.html',
'/img/texture.png',
'/img/math/MAT-ARI-0001.png',
'/img/faviconcolor-fixed.png',
'/img/math/MAT-ARI-0011.png',
'/img/0.png',
'/img/1.png',
'/img/2.png',
'/img/3.png',
'/img/4.png',
'/img/5.png',
'/api/math.json',
'/api/lv1.json',
'/api/lv2.json',
'/api/hg.json',
'/api/svt.json',
'/api/fra.json'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(event.request).then((cachedResponse) => {
// If the request is in the cache and the device is offline, return the cached response
if (cachedResponse && !navigator.onLine) {
return cachedResponse;
}
// If the device is online, fetch the resource from the network
return fetch(event.request).then((networkResponse) => {
// Update the cache with the new response
cache.put(event.request, networkResponse.clone());
return networkResponse;
}).catch(() => {
// If fetching from the network fails, return the cached response (if available)
if (cachedResponse) {
return cachedResponse;
}
});
});
})
);
});