-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
232 lines (232 loc) · 8.18 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function () {
'use strict';
var version = '20231224125927';
var cacheNameHTML = version + '-html';
var cacheNameCSS = version + '-css';
var cacheNameJavaScript = version + '-javascript';
var cacheNameImages = version + '-images';
var cacheNameFonts = version + '-fonts';
var cacheNameFavicons = version + '-favicons';
var cacheNameCatchAll = version + '-catch-all';
var maxCacheHTML = 200;
var maxCacheCSS = 200;
var maxCacheJavaScript = 200;
var maxCacheImages = 200;
var maxCacheFonts = 200;
var maxCacheFavicons = 200;
var maxCacheCatchAll = 200;
var offlineHTML = [];
var offlineCSS = [];
var offlineJavaScript = [];
var offlineImages = [];
var offlineFonts = [];
var offlineFavicons = [];
var offlineCatchAll = [];
function stashInCache(cacheName, request, response) {
caches.open(cacheName).then(function (cache) {
return cache.put(request, response);
}).catch(function (error) {
console.log(error);
return false;
});
}
function updateCaches() {
function cachesOpen(cacheName, offline) {
return caches.open(cacheName).then(function (cache) {
return Promise.all(
offline.map(function (url) {
if (url.length !== 0) {
return cache.add(url).catch(function (reason) {
console.log(String(reason) + ' - ' + url);
return false;
});
} else {
return false;
}
})
).catch(function (error) {
console.log(error);
return false;
});
}).catch(function (error) {
console.log(error);
return false;
});
}
cachesOpen(cacheNameHTML, offlineHTML);
cachesOpen(cacheNameCSS, offlineCSS);
cachesOpen(cacheNameJavaScript, offlineJavaScript);
cachesOpen(cacheNameFonts, offlineFonts);
cachesOpen(cacheNameFavicons, offlineFavicons);
cachesOpen(cacheNameCatchAll, offlineCatchAll);
return cachesOpen(cacheNameImages, offlineImages);
}
function trimCache(cacheName, maxItems) {
caches.open(cacheName).then(function (cache) {
cache.keys().then(function (keys) {
if (keys.length > maxItems) {
cache.delete(keys[0]).then(trimCache(cacheName, maxItems));
}
}).catch(function (error) {
console.log(error);
});
}).catch(function (error) {
console.log(error);
});
}
function clearOldCaches() {
return caches.keys().then(function (keys) {
return Promise.all(keys.filter(function (key) {
return key.indexOf(version) !== 0;
}).map(function (key) {
return caches.delete(key);
})).catch(function (error) {
console.log(error);
return false;
});
}).catch(function (error) {
console.log(error);
return false;
});
}
self.addEventListener('install', function (event) {
event.waitUntil(updateCaches().then(function () {
return self.skipWaiting();
}).catch(function (error) {
console.log(error);
return false;
}));
});
self.addEventListener('activate', function (event) {
event.waitUntil(clearOldCaches().then(function () {
return self.clients.claim();
}).catch(function (error) {
console.log(error);
return false;
}));
});
self.addEventListener('message', function (event) {
if (event.data.command === 'trimCaches') {
trimCache(cacheNameHTML, maxCacheHTML);
trimCache(cacheNameCSS, maxCacheCSS);
trimCache(cacheNameJavaScript, maxCacheJavaScript);
trimCache(cacheNameFonts, maxCacheFonts);
trimCache(cacheNameFavicons, maxCacheFavicons);
trimCache(cacheNameImages, maxCacheImages);
trimCache(cacheNameCatchAll, maxCacheCatchAll);
}
});
self.addEventListener('fetch', function (event) {
var request;
var requestClone;
var responseClone;
var url;
request = event.request;
try {
requestClone = request.clone();
} catch (error) {
console.log(error);
}
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
return;
}
url = new URL(event.request.url);
if (event.request.url.indexOf('google-analytics') !== -1) {
return;
}
if (event.request.url.indexOf('sw.js') !== -1) {
return;
}
if (event.request.url.indexOf('compile') !== -1) {
return;
}
if (event.request.method !== 'GET') {
return;
}
if (navigator.onLine) {
if (event.request.headers.get('Accept') && (event.request.headers.get('Accept').indexOf('text/html') !== -1)) {
event.respondWith(fetch(requestClone, {mode: 'no-cors'}).then(function (response) {
try {
responseClone = response.clone();
if (offlineHTML.includes(url.pathname) || offlineHTML.includes(url.pathname + '/')) {
stashInCache(cacheNameCatchAll, request, responseClone);
} else {
stashInCache(cacheNameHTML, request, responseClone);
}
} catch (error) {
console.log(error);
}
return response;
}).catch(function (error) {
console.log('-----');
console.log(error);
console.log(requestClone.url);
return caches.match(request, {ignoreVary: true}).then(function (response2) {
return response2 || caches.match('./offline/');
}).catch(function (error) {
console.log('-----');
console.log(error);
console.log(request.url);
return new Response("<br><br>Network error", {
"status": 408,
"headers": {
"Content-Type": "text/plain"
}
});
});
}));
return;
}
}
if (event.request.url.indexOf('?') !== -1) {
return;
}
event.respondWith(caches.match(request, {ignoreVary: true}).then(function (response) {
var corsType = 'cors';
if ((request.url.indexOf('https://www.google.com/') !== -1) || (request.url.indexOf('https://www.gstatic.com/') !== -1)) {
corsType = 'no-cors';
}
return response || fetch(requestClone, {mode: corsType}).then(function (response2) {
if (event.request.headers.get('Accept') && (event.request.headers.get('Accept').indexOf('image') !== -1)) {
try {
responseClone = response2.clone();
stashInCache(cacheNameImages, request, responseClone);
} catch (error) {
console.log(error);
}
}
if (event.request.headers.get('Accept') && (event.request.headers.get('Accept').indexOf('text/css') !== -1)) {
try {
responseClone = response2.clone();
stashInCache(cacheNameImages, request, responseClone);
} catch (error2) {
console.log(error2);
}
}
return response2;
}).catch(function (error) {
console.log('=====');
console.log(error);
console.log(request);
if (event.request.headers.get('Accept') && (event.request.headers.get('Accept').indexOf('image') !== -1)) {
return new Response('<svg role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"><title id="offline-title">Offline</title><g fill="none" fill-rule="evenodd"><path fill="#D8D8D8" d="M0 0h400v300H0z"/><text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"><tspan x="93" y="172">offline</tspan></text></g></svg>', {headers: {'Content-Type': 'image/svg+xml'}});
} else {
return new Response("<br><br>Network error", {
"status": 408,
"headers": {
"Content-Type": "text/plain"
}
});
}
});
}).catch(function (error) {
console.log(error);
return new Response("<br><br>Network error", {
"status": 408,
"headers": {
"Content-Type": "text/plain"
}
});
}));
});
}());