-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
83 lines (72 loc) · 2.28 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
'use strict';
(() => {
const cacheVersion = '-sw-v6';
const dynamicVendorCacheName = 'dynamic-vendor' + cacheVersion;
const staticVendorCacheName = 'static-vendor' + cacheVersion;
const staticAssetsCacheName = 'app-assets' + cacheVersion;
const jsCacheName = 'app-js' + cacheVersion;
const cssCacheName = 'app-css' + cacheVersion;
const contentCacheName = 'content' + cacheVersion;
const maxEntries = 50;
const maxAgeSeconds = 3600 * 24 * 3;
self.importScripts('js/sw.js');
self.toolbox.options.debug = false;
self.toolbox.options.networkTimeoutSeconds = 60;
self.toolbox.router.get('/assets/(.*)', self.toolbox.cacheFirst, {
cache: {
name: staticAssetsCacheName,
maxAgeSeconds: maxAgeSeconds,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/css', self.toolbox.fastest, {
origin: /fonts\.googleapis\.com/,
cache: {
name: dynamicVendorCacheName,
maxAgeSeconds: maxAgeSeconds,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/(.*)', self.toolbox.cacheFirst, {
origin: /(fonts\.gstatic\.com|www\.google-analytics\.com|acgers\.github\.io|acgers-1253700126\.cossh\.myqcloud\.com|unpkg\.com)/,
cache: {
name: staticVendorCacheName,
maxAgeSeconds: maxAgeSeconds,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/js/(.*)', self.toolbox.cacheFirst, {
cache: {
name: jsCacheName,
maxAgeSeconds: maxAgeSeconds,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/css/(.*)', self.toolbox.cacheFirst, {
cache: {
name: cssCacheName,
maxAgeSeconds: maxAgeSeconds,
maxEntries: maxEntries
}
});
self.toolbox.router.get('/*', (request, values, options) => {
// !request.url.match(/(\/ghost\/|\/page\/)/) &&
if (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
}
}
);
self.addEventListener('install', event => {
return event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', event => {
return event.waitUntil(self.clients.claim());
});
})();