-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
265 lines (240 loc) · 6.78 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
importScripts('https://cdn.jsdelivr.net/npm/[email protected]/workbox/workbox-sw.js');
workbox.setConfig({
modulePathPrefix: 'https://cdn.jsdelivr.net/npm/[email protected]/workbox/'
});
const { core, precaching, routing, strategies, expiration, cacheableResponse, backgroundSync } = workbox;
const { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } = strategies;
const { ExpirationPlugin } = expiration;
const { CacheableResponsePlugin } = cacheableResponse;
const cacheSuffixVersion = '-201212',
// precacheCacheName = core.cacheNames.precache,
// runtimeCacheName = core.cacheNames.runtime,
maxEntries = 100;
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(keys.map((key) => {
if (key.includes('disqus-cdn-cache')) return caches.delete(key);
if (key.includes('disqus-img-cache')) return caches.delete(key);
if (!key.includes(cacheSuffixVersion)) return caches.delete(key);
}));
})
);
});
core.setCacheNameDetails({
prefix: 'edisonjwa-blog',
suffix: cacheSuffixVersion
});
core.skipWaiting();
core.clientsClaim();
precaching.cleanupOutdatedCaches();
/*
* Precache
* - Prism Highlight
* - Suka Theme
* - Static Assets
*/
precaching.precacheAndRoute(
[
{ url: 'https://static.hit.moe/blog/css/style.min.css', revision: null },
{ url: 'https://static.hit.moe/blog/img/suka-lazyload.gif', revision: null },
{ url: 'https://static.hit.moe/blog/js/local-search.min.js', revision: null },
{ url: 'https://static.hit.moe/blog/js/hanabi-browser.min.js', revision: null },
{ url: 'https://cdn.jsdelivr.net/npm/[email protected]', revision: null },
{ url: 'https://cdn.jsdelivr.net/npm/prism-themes@1/themes/prism-atom-dark.min.css', revision: null },
{ url: 'https://cdn.jsdelivr.net/npm/prismjs@1/plugins/line-numbers/prism-line-numbers.min.css', revision: null },
{ url: 'https://cdn.jsdelivr.net/npm/vanilla-lazyload', revision: null }
],
);
/*
* Cache File From jsDelivr
* cdn.jsdelivr.net | shadow.elemecdn.com
*
* Method: CacheFirst
* cacheName: static-immutable
* cacheTime: 30d
*/
// static.hit.moe - cors enabled
routing.registerRoute(
/.*static\.hit\.moe/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
// cdn.jsdelivr.net - cors enabled
routing.registerRoute(
/.*cdn\.jsdelivr\.net/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
// shadow.elemecdn.com - cors enabled
routing.registerRoute(
/.*shadow\.elemecdn\.com/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
// unpkg.zhimg.com - cors enabled
routing.registerRoute(
/.*unpkg\.zhimg\.com/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
/*
* Google Analytics Async - No Cache
*
* Mathod: networkOnly
*/
routing.registerRoute(
new RegExp('/js/jquery.js/(.*)'),
new NetworkOnly({
plugins: [
new backgroundSync.BackgroundSyncPlugin('Optical_Collect', {
maxRetentionTime: 12 * 60 // Retry for max of 12 Hours (specified in minutes)
}),
]
})
);
routing.registerRoute(
new RegExp('^https://ga\.cdn\.uv\.uy'),
new NetworkOnly({
plugins: [
new backgroundSync.BackgroundSyncPlugin('Optical_Collect', {
maxRetentionTime: 12 * 60 // Retry for max of 12 Hours (specified in minutes)
}),
]
})
);
routing.registerRoute(
new RegExp('/pagead/js/adsbygoogle.js'),
new NetworkOnly({
plugins: [
new backgroundSync.BackgroundSyncPlugin('Optical_Collect', {
maxRetentionTime: 12 * 60 // Retry for max of 12 Hours (specified in minutes)
}),
]
})
);
/*
* Disqus API - No Cache
* disqus.uv.uy | /disqus/
*
* Method: networkOnly
*/
routing.registerRoute(
new RegExp('/disqus/3.0/(.*)'),
new NetworkFirst({
cacheName: 'dsqjs-api' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
networkTimeoutSeconds: 3
})
);
routing.registerRoute(
new RegExp('https://disqus\.uv\.uy'),
new NetworkFirst({
cacheName: 'dsqjs-api' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
networkTimeoutSeconds: 3
})
);
/*
* Disqus Related - No cache
* disqus.com
* *.disquscdn.com
*
* Method: NetworkOnly
*/
routing.registerRoute(
new RegExp('^https://(.*)disqus\.com'),
new NetworkOnly()
);
routing.registerRoute(
new RegExp('^https://(.*)disquscdn\.com(.*)'),
new NetworkOnly()
);
/*
* Others img
* Method: staleWhileRevalidate
* cacheName: img-cache
*/
routing.registerRoute(
// Cache image files
/.*\.(?:png|jpg|jpeg|svg|gif|webp)/,
new StaleWhileRevalidate()
);
/*
* Static Assets
* Method: staleWhileRevalidate
* cacheName: static-assets-cache
*/
routing.registerRoute(
// Cache CSS files
/.*\.(css|js)/,
// Use cache but update in the background ASAP
new StaleWhileRevalidate()
);
/*
* sw.js - Revalidate every time
* staleWhileRevalidate
*/
routing.registerRoute(
'/sw.js',
new StaleWhileRevalidate()
);
/*
* Default - Serve as it is
* networkFirst
*/
routing.setDefaultHandler(
new NetworkFirst({
networkTimeoutSeconds: 3
})
);