hexo-swpp 1.5.1 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/sw-template.js +53 -53
package/package.json
CHANGED
package/sw-template.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
self.addEventListener('install', () => self.skipWaiting())
|
|
10
10
|
|
|
11
|
+
// noinspection JSFileReferences
|
|
11
12
|
const { cacheList, replaceList } = require('../sw-cache')
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -19,7 +20,8 @@
|
|
|
19
20
|
.then(keys => Promise.all(
|
|
20
21
|
keys.map(async it => {
|
|
21
22
|
const url = it.url
|
|
22
|
-
|
|
23
|
+
// noinspection ES6MissingAwait,CommaExpressionJS
|
|
24
|
+
return url !== VERSION_PATH && list.match(url) ? (cache.delete(it), url) : null
|
|
23
25
|
})
|
|
24
26
|
))
|
|
25
27
|
)
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
const url = new URL(request.url)
|
|
32
34
|
if (findCache(url)) {
|
|
33
35
|
event.respondWith(new Promise(async resolve => {
|
|
34
|
-
const key =
|
|
36
|
+
const key = `${url.protocol}//${url.host}${url.pathname}`
|
|
35
37
|
let response = await caches.match(key)
|
|
36
38
|
if (!response) {
|
|
37
39
|
response = await fetchNoCache(request)
|
|
@@ -87,6 +89,7 @@
|
|
|
87
89
|
const value = replaceList[key]
|
|
88
90
|
for (let source of value.source) {
|
|
89
91
|
if (request.url.match(source)) {
|
|
92
|
+
// noinspection JSUnresolvedVariable
|
|
90
93
|
request.url = request.url.replace(source, value.dist)
|
|
91
94
|
flag = true
|
|
92
95
|
}
|
|
@@ -121,14 +124,14 @@
|
|
|
121
124
|
/** 版本号读写操作 */
|
|
122
125
|
const dbVersion = {
|
|
123
126
|
write: (id) => caches.open(CACHE_NAME)
|
|
124
|
-
.then(cache => cache.put(
|
|
125
|
-
read: () => caches.match(
|
|
127
|
+
.then(cache => cache.put(VERSION_PATH, new Response(id))),
|
|
128
|
+
read: () => caches.match(VERSION_PATH).then(response => response?.json())
|
|
126
129
|
}
|
|
127
130
|
let list = new VersionList()
|
|
128
131
|
return dbVersion.read().then(oldVersion => {
|
|
129
|
-
const {
|
|
132
|
+
const {info, global} = json
|
|
130
133
|
const escape = '@$$[escape]'
|
|
131
|
-
const newVersion = {global: global, local:
|
|
134
|
+
const newVersion = {global: global, local: info[0].version, escape: escape}
|
|
132
135
|
//新用户不进行更新操作
|
|
133
136
|
if (!oldVersion) {
|
|
134
137
|
dbVersion.write(JSON.stringify(newVersion))
|
|
@@ -136,7 +139,7 @@
|
|
|
136
139
|
}
|
|
137
140
|
// noinspection JSIncompatibleTypesComparison
|
|
138
141
|
let refresh =
|
|
139
|
-
escape !== 0 && escape !== oldVersion.escape ? true : parseChange(list,
|
|
142
|
+
escape !== 0 && escape !== oldVersion.escape ? true : parseChange(list, info, oldVersion.local)
|
|
140
143
|
dbVersion.write(JSON.stringify(newVersion))
|
|
141
144
|
//如果需要清理全站
|
|
142
145
|
if (refresh) {
|
|
@@ -195,53 +198,50 @@
|
|
|
195
198
|
/**
|
|
196
199
|
* 缓存更新匹配规则表达式
|
|
197
200
|
* @param json 格式{"flag": ..., "value": ...}
|
|
198
|
-
* @see https://kmar.top/posts/bcfe8408/#
|
|
201
|
+
* @see https://kmar.top/posts/bcfe8408/#8dbec4f0
|
|
202
|
+
* @constructor
|
|
199
203
|
*/
|
|
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
|
-
break
|
|
242
|
-
default: throw `未知表达式:${JSON.stringify(json)}`
|
|
243
|
-
}
|
|
204
|
+
function CacheChangeExpression(json) {
|
|
205
|
+
const checkCache = url => {
|
|
206
|
+
const cache = findCache(new URL(url))
|
|
207
|
+
return !cache || cache.clean
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 遍历所有value
|
|
211
|
+
* @param action {function(string): boolean} 接受value并返回bool的函数
|
|
212
|
+
* @return {boolean} 如果value只有一个则返回`action(value)`,否则返回所有运算的或运算(带短路)
|
|
213
|
+
*/
|
|
214
|
+
const forEachValues = action => {
|
|
215
|
+
const value = json.value
|
|
216
|
+
if (Array.isArray(value)) {
|
|
217
|
+
for (let it of value) {
|
|
218
|
+
if (action(it)) return true
|
|
219
|
+
}
|
|
220
|
+
return false
|
|
221
|
+
} else return action(value)
|
|
222
|
+
}
|
|
223
|
+
switch (json['flag']) {
|
|
224
|
+
case 'all':
|
|
225
|
+
this.match = checkCache
|
|
226
|
+
break
|
|
227
|
+
case 'html':
|
|
228
|
+
this.match = url => url.match(/(\/|\/index\.html)$/)
|
|
229
|
+
break
|
|
230
|
+
case 'page':
|
|
231
|
+
this.match = url => forEachValues(
|
|
232
|
+
value => url.endsWith(`/${value}/`) || url.endsWith(`/${value}/index.html`)
|
|
233
|
+
)
|
|
234
|
+
break
|
|
235
|
+
case 'file':
|
|
236
|
+
this.match = url => forEachValues(value => url.endsWith(value))
|
|
237
|
+
break
|
|
238
|
+
case 'str':
|
|
239
|
+
this.match = url => forEachValues(value => url.includes(value))
|
|
240
|
+
break
|
|
241
|
+
case 'reg':
|
|
242
|
+
this.match = url => forEachValues(value => url.match(new RegExp(value, 'i')))
|
|
243
|
+
break
|
|
244
|
+
default: throw `未知表达式:${JSON.stringify(json)}`
|
|
244
245
|
}
|
|
245
|
-
|
|
246
246
|
}
|
|
247
247
|
})()
|