hexo-swpp 1.2.1 → 1.2.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/index.js CHANGED
@@ -141,12 +141,13 @@ const getJsonFromNetwork = async path => {
141
141
  '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'
142
142
  }
143
143
  })
144
- if (result.status < 200 || result.status >= 400 || !result.data)
144
+ if (result.status < 200 || result.status >= 400)
145
145
  // noinspection ExceptionCaughtLocallyJS
146
146
  throw `拉取 ${url} 时出现异常(${result.status})`
147
147
  return await result.json()
148
148
  } catch (e) {
149
- if (e.toString().includes('404'))
149
+ // noinspection SpellCheckingInspection
150
+ if (e.code === 'ENOTFOUND')
150
151
  logger.error(`拉取 ${url} 时出现 404,如果您是第一次构建请忽略这个错误`)
151
152
  else throw e
152
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
@@ -8,7 +8,8 @@
8
8
  },
9
9
  "files": [
10
10
  "index.js",
11
- "sw-template.js"
11
+ "sw-template.js",
12
+ "sw-dom.js"
12
13
  ],
13
14
  "keywords": [
14
15
  "hexo",
package/sw-dom.js ADDED
@@ -0,0 +1,68 @@
1
+ (() => {
2
+ /** 检查 SW 是否可用 */
3
+ const checkServiceWorker = () => 'serviceWorker' in navigator && navigator.serviceWorker.controller
4
+ /** 发送信息到 sw */
5
+ const postMessage2SW = type => navigator.serviceWorker.controller.postMessage(type)
6
+ const pjaxUpdate = url => new Promise(resolve => {
7
+ const type = url.endsWith('js') ? 'script' : 'link'
8
+ const name = type.length === 4 ? 'href' : 'src'
9
+ for (let item of document.querySelectorAll(type)) {
10
+ const itUrl = item[name]
11
+ if (url.length > itUrl ? url.endsWith(itUrl) : itUrl.endsWith(url)) {
12
+ const newEle = document.createElement(type)
13
+ const content = item.text || item.textContent || item.innerHTML || ''
14
+ Array.from(item.attributes).forEach(attr => newEle.setAttribute(attr.name, attr.value))
15
+ newEle.appendChild(document.createTextNode(content))
16
+ item.parentNode.replaceChildren(newEle, item)
17
+ return resolve(true)
18
+ }
19
+ }
20
+ resolve(false)
21
+ })
22
+ const compareUrls = (arg0, arg1) => {
23
+ if (
24
+ (arg0.endsWith('/') && arg1.endsWith('/')) ||
25
+ (arg0.endsWith('/index.html') && arg1.endsWith('/index.html'))
26
+ ) {
27
+ let count = 0
28
+ for (let i = arg0.length - 1, k = arg1.length - 1; arg0[i] === arg1[k]; --i, --k) {
29
+ if (arg0[i] === '/' && ++count === 2) return true
30
+ }
31
+ }
32
+ return false
33
+ }
34
+ if (!checkServiceWorker()) return
35
+ if (sessionStorage.getItem('updated')) {
36
+ // ${onSuccess}
37
+ sessionStorage.removeItem('updated')
38
+ } else setTimeout(() => postMessage2SW('update'), 1000)
39
+ navigator.serviceWorker.addEventListener('message', event => {
40
+ const data = event.data
41
+ switch (data.type) {
42
+ case 'update':
43
+ const list = data.update
44
+ if (!list) break
45
+ sessionStorage.setItem('updated', '1')
46
+ // noinspection JSUnresolvedVariable,JSUnresolvedFunction
47
+ if (Pjax?.isSupported()) {
48
+ Promise.all(list.map(url => {
49
+ if (url.endsWith('.js'))
50
+ return pjaxUpdate(url)
51
+ if (url.endsWith('.css'))
52
+ return pjaxUpdate(url)
53
+ return Promise.resolve(url.endsWith('.json') || compareUrls(url, location.href))
54
+ })).then(list => {
55
+ for (let it of list) {
56
+ if (it) return location.reload()
57
+ }
58
+ sessionStorage.removeItem('updated')
59
+ // ${onSuccess}
60
+ })
61
+ } else location.reload()
62
+ break
63
+ case 'refresh':
64
+ location.reload()
65
+ break
66
+ }
67
+ })
68
+ })
package/sw-template.js CHANGED
@@ -264,7 +264,7 @@
264
264
  this.match = checkCache
265
265
  break
266
266
  case 'html':
267
- this.match = url => url.href.match(/(\/|\/index\.html)$/)
267
+ this.match = url => url.match(/(\/|\/index\.html)$/)
268
268
  break
269
269
  case 'page':
270
270
  this.match = url => forEachValues(