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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/sw-template.js +53 -53
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
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
- return url !== VERSION_PATH && list.match(url) ? cache.delete(it) : null
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 = new Request(`${url.protocol}//${url.host}${url.pathname}`)
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(new Request(VERSION_PATH), new Response(id))),
125
- read: () => caches.match(new Request(VERSION_PATH)).then(response => response.json())
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 {elementList, global} = json
132
+ const {info, global} = json
130
133
  const escape = '@$$[escape]'
131
- const newVersion = {global: global, local: elementList[0].version, escape: escape}
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, elementList, oldVersion.local)
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/#JSON格式
201
+ * @see https://kmar.top/posts/bcfe8408/#8dbec4f0
202
+ * @constructor
199
203
  */
200
- class CacheChangeExpression {
201
-
202
- constructor(json) {
203
- const checkCache = url => {
204
- const cache = findCache(new URL(url))
205
- return !cache || cache.clean
206
- }
207
- /**
208
- * 遍历所有value
209
- * @param action {function(string): boolean} 接受value并返回bool的函数
210
- * @return {boolean} 如果value只有一个则返回`action(value)`,否则返回所有运算的或运算(带短路)
211
- */
212
- const forEachValues = action => {
213
- const value = json.value
214
- if (Array.isArray(value)) {
215
- for (let it of value) {
216
- if (action(it)) return true
217
- }
218
- return false
219
- } else return action(value)
220
- }
221
- switch (json['flag']) {
222
- case 'all':
223
- this.match = checkCache
224
- break
225
- case 'html':
226
- this.match = url => url.match(/(\/|\/index\.html)$/)
227
- break
228
- case 'page':
229
- this.match = url => forEachValues(
230
- value => url.endsWith(`/${value}/`) || url.endsWith(`/${value}/index.html`)
231
- )
232
- break
233
- case 'file':
234
- this.match = url => forEachValues(value => url.endsWith(value))
235
- break
236
- case 'str':
237
- this.match = url => forEachValues(value => url.includes(value))
238
- break
239
- case 'reg':
240
- this.match = url => forEachValues(value => url.match(new RegExp(value, 'i')))
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
  })()