-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
102 lines (89 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var cacheName = 'hugo-nuo-v5';
var filesToCache = [
'404.html',
'favicon.ico',
'manifest.json',
'icons/icon-16x16.png',
'icons/icon-32x32.png',
'icons/icon-128x128.png',
'icons/icon-144x144.png',
'icons/icon-152x152.png',
'icons/icon-192x192.png',
'icons/icon-256x256.png',
'icons/icon-512x512.png',
'images/avatar.png',
'images/grey-prism.svg',
'images/qrcode.jpg',
'styles/main-rendered.min.css',
'scripts/index.min.js',
// Google fonts
'https://fonts.googleapis.com/css?family=Lobster',
'https://fonts.gstatic.com/s/lobster/v20/neILzCirqoswsqX9zoKmM4MwWJU.woff2',
// Iconfont
'https://at.alicdn.com/t/font_174169_qmgvd10zwbf.woff',
// smooth-scroll
'https://cdn.jsdelivr.net/npm/[email protected]/dist/smooth-scroll.min.js',
// medium-zoom
'https://cdn.jsdelivr.net/npm/[email protected]/dist/medium-zoom.min.js',
// Video.js
'https://cdn.jsdelivr.net/npm/[email protected]/dist/video-js.min.css',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/video.min.js',
// MathJax
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML',
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js?V=2.7.5',
];
// Cache the application assets
self.addEventListener('install', event => {
event.waitUntil(caches.open(cacheName).then(cache => cache.addAll(filesToCache)));
});
// network first
self.addEventListener('fetch', event => {
event.respondWith(
caches.open(cacheName).then(function(cache) {
return fetch(event.request)
.then(function(response) {
if (response.status === 404) return caches.match('404.html');
cache.put(event.request, response.clone());
return response;
})
.catch(function() {
return caches.match(event.request);
});
}),
);
});
// cache-first
// If you want to use cache first, you should change cacheName manually
// self.addEventListener('fetch', event => {
// event.respondWith(
// caches
// .match(event.request)
// .then(response => {
// if (response) return response;
// return fetch(event.request);
// })
// .then(response => {
// if (response.status === 404) return caches.match('404.html');
// return caches.open(cacheName).then(cache => {
// cache.put(event.request.url, response.clone());
// return response;
// });
// })
// .catch(error => console.log('Error, ', error)),
// );
// });
// Delete outdated caches
self.addEventListener('activate', event => {
const cacheWhitelist = [cacheName];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
}),
);
}),
);
});