hexo-swpp 1.5.3 → 1.5.4

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 +47 -35
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
package/sw-template.js CHANGED
@@ -23,7 +23,7 @@
23
23
  // noinspection ES6MissingAwait,CommaExpressionJS
24
24
  return url !== VERSION_PATH && list.match(url) ? (cache.delete(it), url) : null
25
25
  })
26
- ))
26
+ )).then(list => list.filter(it => it))
27
27
  )
28
28
 
29
29
  self.addEventListener('fetch', event => {
@@ -32,19 +32,16 @@
32
32
  const replace = replaceRequest(request)
33
33
  const url = new URL(request.url)
34
34
  if (findCache(url)) {
35
- event.respondWith(new Promise(async resolve => {
36
- const key = `${url.protocol}//${url.host}${url.pathname}`
37
- let response = await caches.match(key)
38
- if (!response) {
39
- response = await fetchNoCache(request)
40
- const status = response.status
41
- if ((status > 199 && status < 400) || status === 0) {
35
+ const key = `${url.protocol}//${url.host}${url.pathname}`
36
+ event.respondWith(caches.match(key).then(cache =>
37
+ cache ? cache : fetchNoCache(request).then(response => {
38
+ if (response.ok) {
42
39
  const clone = response.clone()
43
- caches.open(CACHE_NAME).then(cache => cache.put(key, clone))
40
+ caches.open(CACHE_NAME).then(it => it.put(key, clone))
44
41
  }
45
- }
46
- resolve(response)
47
- }))
42
+ return response
43
+ })
44
+ ))
48
45
  } else if (replace) {
49
46
  event.respondWith(fetch(request))
50
47
  }
@@ -52,14 +49,14 @@
52
49
 
53
50
  self.addEventListener('message', event => {
54
51
  if (event.data === 'update') {
55
- updateJson().then(info => {
52
+ updateJson().then(info =>
56
53
  // noinspection JSUnresolvedVariable
57
54
  event.source.postMessage({
58
55
  type: 'update',
59
- update: info.update,
56
+ update: info.list,
60
57
  version: info.version,
61
58
  })
62
- })
59
+ )
63
60
  }
64
61
  })
65
62
 
@@ -100,7 +97,7 @@
100
97
 
101
98
  /**
102
99
  * 根据JSON删除缓存
103
- * @returns {Promise<boolean>} 返回值用于标记当前页是否被刷新
100
+ * @returns {Promise<{version, list}>}
104
101
  */
105
102
  function updateJson() {
106
103
  /**
@@ -124,7 +121,7 @@
124
121
  /** 版本号读写操作 */
125
122
  const dbVersion = {
126
123
  write: (id) => caches.open(CACHE_NAME)
127
- .then(cache => cache.put(VERSION_PATH, new Response(id))),
124
+ .then(cache => cache.put(VERSION_PATH, new Response(JSON.stringify(id)))),
128
125
  read: () => caches.match(VERSION_PATH).then(response => response?.json())
129
126
  }
130
127
  let list = new VersionList()
@@ -134,19 +131,18 @@
134
131
  const newVersion = {global: global, local: info[0].version, escape: escape}
135
132
  //新用户不进行更新操作
136
133
  if (!oldVersion) {
137
- dbVersion.write(JSON.stringify(newVersion))
134
+ dbVersion.write(newVersion)
138
135
  return newVersion
139
136
  }
140
137
  // noinspection JSIncompatibleTypesComparison
141
138
  let refresh =
142
139
  escape !== 0 && escape !== oldVersion.escape ? true : parseChange(list, info, oldVersion.local)
143
- dbVersion.write(JSON.stringify(newVersion))
140
+ dbVersion.write(newVersion)
144
141
  //如果需要清理全站
145
142
  if (refresh) {
146
- if (global === oldVersion.global) {
147
- list._list.length = 0
148
- list.push(new CacheChangeExpression({'flag': 'all'}))
149
- } else list.refresh = true
143
+ if (global === oldVersion.global)
144
+ list.clean(new CacheChangeExpression({'flag': 'all'}))
145
+ else list.refresh = true
150
146
  }
151
147
  return {list: list, version: newVersion}
152
148
  })
@@ -158,7 +154,7 @@
158
154
  parseJson(json).then(result => result.list ?
159
155
  deleteCache(result.list).then(list => {
160
156
  return {
161
- update: list.filter(it => it),
157
+ list,
162
158
  version: result.version
163
159
  }
164
160
  }) : {version: result}
@@ -168,25 +164,41 @@
168
164
  })
169
165
  }
170
166
 
171
- /** 版本列表 */
172
- class VersionList {
167
+ /**
168
+ * 版本列表
169
+ * @constructor
170
+ */
171
+ function VersionList() {
173
172
 
174
- _list = []
175
- refresh = false
173
+ const list = []
174
+ const refresh = false
176
175
 
177
- push(element) {
178
- this._list.push(element)
176
+ /**
177
+ * 推送一个表达式
178
+ * @param element {CacheChangeExpression} 要推送的表达式
179
+ */
180
+ this.push = element => {
181
+ list.push(element)
179
182
  }
180
183
 
181
- clean(element = null) {
182
- this._list.length = 0
184
+ /**
185
+ * 清除列表,并将指定元素推入列表中
186
+ * @param element {CacheChangeExpression} 要推入的元素,留空表示不推入
187
+ */
188
+ this.clean = element => {
189
+ list.length = 0
183
190
  if (!element) this.push(element)
184
191
  }
185
192
 
186
- match(url) {
187
- if (this.refresh) return true
193
+ /**
194
+ * 判断指定 URL 是否和某一条规则匹配
195
+ * @param url {string} URL
196
+ * @return {boolean}
197
+ */
198
+ this.match = url => {
199
+ if (refresh) return true
188
200
  else {
189
- for (let it of this._list) {
201
+ for (let it of list) {
190
202
  if (it.match(url)) return true
191
203
  }
192
204
  }