-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
63 lines (51 loc) · 1.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
'use strict';
(function () {
'use strict';
var cacheVersion = '-toolbox-v1';
var dynamicVendorCacheName = 'dynamic-vendor' + cacheVersion;
var staticVendorCacheName = 'static-vendor' + cacheVersion;
var staticAssetsCacheName = 'static-assets' + cacheVersion;
var contentCacheName = 'content' + cacheVersion;
var maxEntries = 50;
self.importScripts('assets/js/sw-toolbox.js');
self.toolbox.options.debug = false;
// Cache own static assets
self.toolbox.router.get('/assets/(.*)', self.toolbox.cacheFirst, {
cache: {
name: staticAssetsCacheName,
maxEntries: maxEntries
}
});
// Do not cache changyan
self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, {
origin: /sohu\.com/
});
self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, {
origin: /itc\.cn/
});
self.toolbox.router.get('/content/(.*)', self.toolbox.fastest, {
cache: {
name: contentCacheName,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/*', function (request, values, options) {
if (!request.url.match(/(\/ghost\/|\/page\/)/) && request.headers.get('accept').includes('text/html')) {
return self.toolbox.fastest(request, values, options);
} else {
return self.toolbox.networkOnly(request, values, options);
}
}, {
cache: {
name: contentCacheName,
maxEntries: maxEntries
}
});
// immediately activate this serviceworker
self.addEventListener('install', function (event) {
return event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function (event) {
return event.waitUntil(self.clients.claim());
});
})();