-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
151 lines (135 loc) · 4.82 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
importScripts('https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js');
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉`);
workbox.setConfig({debug: false});
version = "v5";
versionTag = ".4";
versionName = version + versionTag;
workbox.core.setCacheNameDetails({
prefix: "Reborn",
suffix: versionName,
precache: 'precache',
runtime: 'runtime'
});
// 跳过等待期
workbox.core.skipWaiting();
// 一旦激活就开始控制任何现有客户机(通常是与skipWaiting配合使用)
workbox.core.clientsClaim();
// 删除过期缓存
workbox.precaching.cleanupOutdatedCaches();
workbox.precaching.precacheAndRoute([
{
"url": "/index.html",
"revision": "1c8bd1904eff2cd594b79de84026ade8"
},
{
"url": "https://cdn.jsdelivr.net/gh/RebornQ/rebornQ.github.io/css/style.min.css",
"revision": "9b22538f7e96295fd4cd36aa08f36959"
},
{
"url": "/svg/loading.min.svg",
"revision": "4055d59e164001e9375d89ed9ddc38f2"
}
], {});
workbox.routing.registerRoute(/(?:\/)$/,
new workbox.strategies.NetworkFirst({
cacheName: "html-" + versionName,
plugins: [
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 2,
// purgeOnQuotaError: !0
})
]
}), "GET");
workbox.routing.registerRoute(
/\.(?:js|css)$/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'static-resources-' + versionName
})
);
workbox.routing.registerRoute(
/\.(?:png|jpg|jpeg|gif|bmp|webp|svg|ico)$/,
new workbox.strategies.CacheFirst({
cacheName: "images-" + versionName,
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 100,
maxAgeSeconds: 7 * 24 * 60 * 60,
// purgeOnQuotaError: !0
})
]
}), "GET");
// Fonts
workbox.routing.registerRoute(
/\.(?:eot|ttf|woff|woff2)$/,
new workbox.strategies.CacheFirst({
cacheName: "fonts-" + versionName,
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 1000,
maxAgeSeconds: 60 * 60 * 24 * 30
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
})
]
})
);
workbox.routing.registerRoute(
/^https:\/\/fonts\.googleapis\.com/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'google-fonts-stylesheets-' + versionName
})
);
workbox.routing.registerRoute(
/^https:\/\/fonts\.gstatic\.com/,
new workbox.strategies.CacheFirst({
cacheName: 'google-fonts-webfonts-' + versionName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30
})
]
})
);
// external resources
/*workbox.routing.registerRoute(
/(^https:\/\/cdn\.jsdelivr\.net.*?(\.js|\.css))|(^https:\/\/cdnjs\.cloudflare\.com)/,
new workbox.strategies.CacheFirst({
cacheName: "external-resources-" + versionName,
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 1000,
maxAgeSeconds: 60 * 60 * 24 * 30
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
})
]
})
);*/
workbox.googleAnalytics.initialize({});
// Call Activate Event to remove old cache
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (cacheList) {
return Promise.all(
cacheList.map(function (cacheName) {
if (/(v[\d+.]+)/.test(cacheName) === false || versionName !== RegExp.$1) {
// When it doesn't match any condition, delete it.
console.info('version changed, clean the cache, SW: deleting ' + cacheName);
return caches.delete(cacheName);
}
})
);
})
);
return self.clients.claim();
});
} else {
console.log(`Boo! Workbox didn't load 😬`)
}