hexo-swpp 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -10,11 +10,13 @@
10
10
  swpp:
11
11
  enable: true
12
12
  # 是否使用自定义的 sw,为 true 时不自动生成 sw.js,但是仍然会插入注册 sw 的代码
13
- # 注:不支持自定义 sw.js 的路径及文件名,sw.js 必须放置在 source_dir 中
13
+ # 注意:不支持自定义 sw.js 的路径及文件名,sw.js 必须放置在 source_dir 中
14
14
  customJS: false
15
15
  # 注册 sw 发生错误时触发的 js 代码,如果包含多个指令需使用花括号({})包裹
16
+ # 注意:SW 注册代码将直接内嵌在 HTML 首部,该代码执行时其它 JS 不一定完成了加载
16
17
  onError: "document.addEventListener('DOMContentLoaded', () => kms.pushInfo('当前浏览器不支持SW,建议更换浏览器以获取最佳体验~'))"
17
18
  # 注册 sw 成功后触发的 js 代码,如果包含多个指令需使用花括号({})包裹
19
+ # 注意:SW 注册代码将直接内嵌在 HTML 首部,该代码执行时其它 JS 不一定完成了加载
18
20
  onSuccess: "location.reload()"
19
21
  # 最大 HTML 数量,超过这个数量后会直接清除所有 HTML 缓存
20
22
  maxHtml: 15
@@ -43,4 +45,6 @@ swpp:
43
45
  exclude:
44
46
  # 这里写正则表达式,格式如下:
45
47
  - sw\.js
46
- ```
48
+ ```
49
+
50
+   具体说明见:[小白也能用的 SW 构建插件 - 山岳库博](https://kmar.top/posts/73014407/)
package/index.js CHANGED
@@ -117,7 +117,12 @@ const buildNewJson = path => {
117
117
  const getJsonFromNetwork = async path => {
118
118
  const url = root + path
119
119
  try {
120
- const result = await axios.get(url)
120
+ const result = await axios.get(url, {
121
+ headers: {
122
+ referer: new URL(url).hostname,
123
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.62'
124
+ }
125
+ })
121
126
  if (result.status < 200 || result.status >= 400 || !result.data)
122
127
  // noinspection ExceptionCaughtLocallyJS
123
128
  throw `拉取 ${url} 时出现异常(${result.status})`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "axios": "^1.2.0",
package/sw-template.js CHANGED
@@ -121,7 +121,6 @@
121
121
  * @return boolean 是否刷新全站缓存
122
122
  */
123
123
  const parseChange = (list, elements, version) => {
124
- let biliFlag = false
125
124
  let result = true
126
125
  for (let element of elements) {
127
126
  const ver = element['version']
@@ -129,14 +128,12 @@
129
128
  result = false
130
129
  break
131
130
  }
132
- if (ver.endsWith('b')) biliFlag = true
133
131
  const jsonList = element['change']
134
132
  if (jsonList) {
135
133
  for (let it of jsonList)
136
134
  list.push(new CacheChangeExpression(it))
137
135
  }
138
136
  }
139
- if (biliFlag) list.push(new CacheChangeExpression({flag: "str", value: "/bilibili/"}))
140
137
  // resul=true时表明读取了已存在的所有版本信息后依然没有找到客户端当前的版本号
141
138
  // 说明跨版本幅度过大,直接清理全站
142
139
  return result
@@ -184,7 +181,7 @@
184
181
  } else list.refresh = true
185
182
  }
186
183
  resolve({list: list, version: newVersion})
187
- }).catch(() => dbVersion.write('{"global":0, "local":"-"}'))
184
+ }).catch(() => dbVersion.write('{"global":0, "local": -1}'))
188
185
  })
189
186
  const url = `/update.json` //需要修改JSON地址的在这里改
190
187
  return new Promise(resolve => fetchNoCache(url)
@@ -262,25 +259,18 @@
262
259
  case 'all':
263
260
  this.match = checkCache
264
261
  break
265
- case 'post':
266
- this.match = url => url.endsWith('postsInfo.json') ||
267
- forEachValues(post => url.endsWith(`posts/${post}/`))
268
- break
269
262
  case 'html':
270
- this.match = url => cacheList.html.match(new URL(url)) || url.endsWith('postsInfo.json')
263
+ this.match = url => url.href.match(/(\/|\/index\.html)$/)
271
264
  break
272
265
  case 'file':
273
266
  this.match = url => forEachValues(value => url.endsWith(value))
274
267
  break
275
- case 'new':
276
- this.match = url => url.endsWith('postsInfo.json') || url.match(/\/archives\//)
277
- break
278
- case 'page':
279
- this.match = url => forEachValues(value => url.match(new RegExp(`\/${value}(\/|)$`)))
280
- break
281
268
  case 'str':
282
269
  this.match = url => forEachValues(value => url.includes(value))
283
270
  break
271
+ case 'reg':
272
+ this.match = url => forEachValues(value => url.match(new RegExp(value, 'i')))
273
+ break
284
274
  default: throw `未知表达式:${JSON.stringify(json)}`
285
275
  }
286
276
  }