-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
91 lines (85 loc) · 3.17 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
83
84
85
86
87
88
89
90
91
var cacheName = 'mypwa1';
var filesToCache = [
'/',
'/index.html',
'/404.html',
'/about.html',
'/sitemap.html',
'/css/site.css',
'/css/prism.css',
'/css/fonts/firasans/fira.css',
'/css/fonts/firasans/woff/FiraSans-LightItalic.woff',
'/css/fonts/firasans/woff/FiraSans-BoldItalic.woff',
'/css/fonts/firasans/woff/FiraSans-MediumItalic.woff',
'/css/fonts/firasans/woff/FiraSans-Regular.woff',
'/css/fonts/firasans/woff/FiraSans-RegularItalic.woff',
'/css/fonts/firasans/woff/FiraSans-Light.woff',
'/css/fonts/firasans/woff/FiraSans-Medium.woff',
'/css/fonts/firasans/woff/FiraSans-Bold.woff',
'/css/fonts/firasans/ttf/FiraSans-RegularItalic.ttf',
'/css/fonts/firasans/ttf/FiraSans-LightItalic.ttf',
'/css/fonts/firasans/ttf/FiraSans-MediumItalic.ttf',
'/css/fonts/firasans/ttf/FiraSans-Regular.ttf',
'/css/fonts/firasans/ttf/FiraSans-Bold.ttf',
'/css/fonts/firasans/ttf/FiraSans-Medium.ttf',
'/css/fonts/firasans/ttf/FiraSans-Light.ttf',
'/css/fonts/firasans/ttf/FiraSans-BoldItalic.ttf',
'/css/fonts/firasans/eot/FiraSans-BoldItalic.eot',
'/css/fonts/firasans/eot/FiraSans-Light.eot',
'/css/fonts/firasans/eot/FiraSans-Regular.eot',
'/css/fonts/firasans/eot/FiraSans-Medium.eot',
'/css/fonts/firasans/eot/FiraSans-RegularItalic.eot',
'/css/fonts/firasans/eot/FiraSans-Bold.eot',
'/css/fonts/firasans/eot/FiraSans-MediumItalic.eot',
'/css/fonts/firasans/eot/FiraSans-LightItalic.eot',
'/js/main.js',
'/notes/index.html',
'/notes/sciences.html',
'/notes/rando.html',
'/notes/informatique.html',
'/notes/lecture.html',
'/notes/musique.html',
'/notes/glossaire.html'
];
/* Start the service worker and cache all of the app's content */
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
/* Serve cached content when offline */
// self.addEventListener('fetch', function(e) {
// e.respondWith(
// caches.match(e.request).then(function(response) {
// return response || fetch(e.request);
// })
// );
// });
/* Stale-while-revalidate */
// https://jakearchibald.com/2014/offline-cookbook/#stale-while-revalidate
self.addEventListener('fetch', (event) => {
event.respondWith(async function() {
const cache = await caches.open(cacheName);
const cachedResponse = await cache.match(event.request);
const networkResponsePromise = fetch(event.request);
event.waitUntil(async function() {
const networkResponse = await networkResponsePromise;
await cache.put(event.request, networkResponse.clone());
}());
// Returned the cached response if we have one, otherwise return the network response.
return cachedResponse || networkResponsePromise;
}());
});
/* Network falling back to cache */
// https://jakearchibald.com/2014/offline-cookbook/#network-falling-back-to-cache
// self.addEventListener('fetch', (event) => {
// event.respondWith(async function() {
// try {
// return await fetch(event.request);
// } catch (err) {
// return caches.match(event.request);
// }
// }());
// });