-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
154 lines (136 loc) · 3.85 KB
/
sw.js
File metadata and controls
154 lines (136 loc) · 3.85 KB
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
// 引入workbox,这里使用镜像缓解大陆用户的防火墙
importScripts('https://cdn.jsdelivr.net/npm/workbox-cdn@5.1.3/workbox/workbox-sw.js');
workbox.setConfig({
modulePathPrefix: 'https://cdn.jsdelivr.net/npm/workbox-cdn@5.1.3/workbox/'
});
// 设定缓存版本号,若有重大更新请务必更新此版本号
const cacheSuffixVersion = '-ver-0000-0000-0099-0061',
// 最大缓存文件数目,防止写爆缓存
maxEntries = 100;
self.addEventListener(
'activate', () => {
caches.keys().then(keys => {
return Promise.all(keys.map(key => {
// 删除不包含该版本号的所有缓存
if (!key.includes(cacheSuffixVersion)) return caches.delete(key);
}))
});
}
)
// 设定简易名称
const { core, precaching, routing, strategies, expiration, cacheableResponse, backgroundSync } = workbox;
const { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } = strategies;
const { ExpirationPlugin } = expiration;
const { CacheableResponsePlugin } = cacheableResponse;
//设定缓存名规则
core.setCacheNameDetails({
prefix: 'nmfun-cache',
suffix: cacheSuffixVersion
});
core.skipWaiting();
core.clientsClaim();
precaching.cleanupOutdatedCaches();
// 预缓存文件
workbox.precaching.precacheAndRoute([
{
"url": "/src/js/jquery.min.js",
"revision": null
}
]);
/*
* 跨域静态资源缓存
* 规则类型: cacheFirst
* 缓存名: cache-static-lib
*/
routing.registerRoute(/.*cdn\.jsdelivr\.net/, new CacheFirst({
cacheName: "cache-static-lib" + cacheSuffixVersion,
fetchOptions: {
mode: "cors",
credentials: "omit"
},
plugins: [new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})]
}));
/*
* 跨域静态资源
* 规则类型: cacheFirst
* 缓存名: cache-static-img
*/
// routing.registerRoute(/.*a\.b\.c.*/, new CacheFirst({
// cacheName: "cache-static-img" + cacheSuffixVersion,
// fetchOptions: {
// mode: "cors",
// credentials: "omit"
// },
// plugins: [new ExpirationPlugin({
// maxAgeSeconds: 30 * 24 * 60 * 60,
// purgeOnQuotaError: true
// })]
// }));
/*
* 跨域API资源
* 规则类型: cacheFirst
* 缓存名: cache-static-img
*/
routing.registerRoute(/.*funapi\.nmteam\.xyz.*/, new CacheFirst({
cacheName: "cache-static-api" + cacheSuffixVersion,
fetchOptions: {
mode: "cors",
credentials: "omit"
},
plugins: [new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})]
}));
routing.registerRoute(/.*funapibeta\.nmteam\.xyz.*/, new CacheFirst({
cacheName: "cache-static-api" + cacheSuffixVersion,
fetchOptions: {
mode: "cors",
credentials: "omit"
},
plugins: [new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})]
}));
// 不强制缓存配置文件,以免翻车
routing.registerRoute(
'/sw.js',
new StaleWhileRevalidate()
);
// Analytics gtag.js
routing.registerRoute(
/.*googletagmanager\.com/,
new StaleWhileRevalidate()
);
// Gravatar no cache
routing.registerRoute(
/.*sdn\.geekzu\.org/,
new StaleWhileRevalidate()
);
/*
* 同域静态资源
* 规则类型: cacheFirst
* 缓存名: 无
*/
workbox.routing.registerRoute(
new RegExp('.*\.(css|js)'),
new workbox.strategies.StaleWhileRevalidate()
);
workbox.routing.registerRoute(
new RegExp('.*\.(?:|svg|ico|webp)'),
new workbox.strategies.StaleWhileRevalidate()
);
workbox.routing.registerRoute(
new RegExp('.*\.html'),
new workbox.strategies.StaleWhileRevalidate()
);
workbox.routing.setDefaultHandler(
new workbox.strategies.NetworkFirst({
// 请求超过2秒未应答则切换到本地缓存
networkTimeoutSeconds: 2
})
);