hexo-swpp 1.1.5 → 1.2.0
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 +2 -47
- package/index.js +28 -11
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,50 +1,5 @@
|
|
|
1
|
-
# 介绍
|
|
2
|
-
|
|
3
1
|
  该插件用于在 hexo 中自动构建可用的 ServiceWorker,插件内自带了一个缺省的 sw.js,也支持使用自定义的 sw。
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
  `enable`、`onError`及`onSuccess`为必填项(无缺省设置),其余为可选项,可选项的缺省设置为下方列出的设置。
|
|
8
|
-
|
|
9
|
-
```yaml
|
|
10
|
-
swpp:
|
|
11
|
-
enable: true
|
|
12
|
-
# 是否使用自定义的 sw,为 true 时不自动生成 sw.js,但是仍然会插入注册 sw 的代码
|
|
13
|
-
# 注意:不支持自定义 sw.js 的路径及文件名,sw.js 必须放置在 source_dir 中
|
|
14
|
-
customJS: false
|
|
15
|
-
# 注册 sw 发生错误时触发的 js 代码,如果包含多个指令需使用花括号({})包裹
|
|
16
|
-
# 注意:SW 注册代码将直接内嵌在 HTML 首部,该代码执行时其它 JS 不一定完成了加载
|
|
17
|
-
onError: "document.addEventListener('DOMContentLoaded', () => kms.pushInfo('当前浏览器不支持SW,建议更换浏览器以获取最佳体验~'))"
|
|
18
|
-
# 注册 sw 成功后触发的 js 代码,如果包含多个指令需使用花括号({})包裹
|
|
19
|
-
# 注意:SW 注册代码将直接内嵌在 HTML 首部,该代码执行时其它 JS 不一定完成了加载
|
|
20
|
-
onSuccess: "location.reload()"
|
|
21
|
-
# 最大 HTML 数量,超过这个数量后会直接清除所有 HTML 缓存
|
|
22
|
-
maxHtml: 15
|
|
23
|
-
# update.json 的最大字符数量
|
|
24
|
-
# 超过后会移除旧的版本号,直到满足要求,如果只有全部清空才能满足就会直接刷新所有缓存
|
|
25
|
-
charLimit: 1024
|
|
26
|
-
# 文件缓存匹配采取精确模式
|
|
27
|
-
# 关闭时更新缓存时仅匹配文件名称,如 https://kmar.top/simple/a/index.html 仅匹配 /a/index.html
|
|
28
|
-
# 开启后更新缓存时将会匹配完整名称,如 https://kmar.top/simple/a/index.html 将匹配 /simple/a/index.html
|
|
29
|
-
# 两种方式各有优劣,开启后会增加 update.json 的空间占用,但会提升精确度
|
|
30
|
-
# 如果网站内没有多级目录结构,就可以放心大胆的关闭了
|
|
31
|
-
# key 值为文件拓展名,default 用于指代所有未列出的拓展名以及没有拓展名的文件
|
|
32
|
-
precisionMode:
|
|
33
|
-
default: false
|
|
34
|
-
# 是否合并指定项目
|
|
35
|
-
# 例如当 tags 为 true 时(假设标签目录为 https://kmar.top/tags/...)
|
|
36
|
-
# 如果标签页存在更新,则直接匹配 https://kmar.top/tags/ 目录下的所有文件
|
|
37
|
-
# 推荐将此项开启
|
|
38
|
-
merge:
|
|
39
|
-
tags: true
|
|
40
|
-
archives: true
|
|
41
|
-
categories: true
|
|
42
|
-
index: true
|
|
43
|
-
# 忽略哪些文件,正则表达式,不写两边的斜杠,不区分大小写
|
|
44
|
-
# 注:匹配的时候不附带域名,只有 pathname
|
|
45
|
-
exclude:
|
|
46
|
-
# 这里写正则表达式,格式如下:
|
|
47
|
-
- sw\.js
|
|
48
|
-
```
|
|
3
|
+
  具体说明见:[小白也能用的 SW 构建插件 - 山岳库博](https://kmar.top/posts/73014407/)
|
|
49
4
|
|
|
50
|
-
 &emsp
|
|
5
|
+
  `1.2.0`之前的版本存在功能缺陷,请勿使用!
|
package/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const fs = require('fs')
|
|
6
6
|
const logger = require('hexo-log')()
|
|
7
|
-
const
|
|
7
|
+
const fetch = require('node-fetch')
|
|
8
8
|
const path = require('path')
|
|
9
9
|
|
|
10
10
|
const findScript = () => path.resolve('./', 'sw-cache')
|
|
@@ -30,7 +30,7 @@ if (pluginConfig?.enable) {
|
|
|
30
30
|
|
|
31
31
|
// 生成 sw.js
|
|
32
32
|
hexo.extend.generator.register('buildSw', () => {
|
|
33
|
-
if (pluginConfig.
|
|
33
|
+
if (pluginConfig.sw.custom) return
|
|
34
34
|
const absPath = module.path + '/sw-template.js'
|
|
35
35
|
const rootPath = path.resolve('./')
|
|
36
36
|
const relativePath = path.relative(rootPath, absPath)
|
|
@@ -40,7 +40,8 @@ if (pluginConfig?.enable) {
|
|
|
40
40
|
.replace('module.exports.replaceList', 'const replaceList')
|
|
41
41
|
return {
|
|
42
42
|
path: 'sw.js',
|
|
43
|
-
data: template.replace('const { cacheList, replaceList } = require(\'../sw-cache\')', cache)
|
|
43
|
+
data: template.replace('const { cacheList, replaceList } = require(\'../sw-cache\')', cache),
|
|
44
|
+
layout: 'js'
|
|
44
45
|
}
|
|
45
46
|
})
|
|
46
47
|
|
|
@@ -49,13 +50,32 @@ if (pluginConfig?.enable) {
|
|
|
49
50
|
return `<script>
|
|
50
51
|
(() => {
|
|
51
52
|
const sw = navigator.serviceWorker
|
|
52
|
-
const error = () => ${pluginConfig.
|
|
53
|
+
const error = () => ${pluginConfig.sw.onerror}
|
|
53
54
|
if (!sw?.register('/sw.js')?.then(() => {
|
|
54
|
-
if (!sw.controller) ${pluginConfig.
|
|
55
|
+
if (!sw.controller) ${pluginConfig.sw.onsuccess}
|
|
55
56
|
})?.catch(error)) error()
|
|
56
57
|
})()
|
|
57
58
|
</script>`
|
|
58
59
|
}, "default")
|
|
60
|
+
|
|
61
|
+
if (!pluginConfig.dom?.custom) {
|
|
62
|
+
hexo.extend.injector.register('body_begin', () => {
|
|
63
|
+
// noinspection HtmlUnknownTarget
|
|
64
|
+
return `<script src="/js/sw-dom.js"></script>`
|
|
65
|
+
})
|
|
66
|
+
hexo.extend.generator.register('buildDomJs', () => {
|
|
67
|
+
const absPath = module.path + '/sw-dom.js'
|
|
68
|
+
const rootPath = path.resolve('./')
|
|
69
|
+
const relativePath = path.relative(rootPath, absPath)
|
|
70
|
+
const template = fs.readFileSync(relativePath, 'utf-8')
|
|
71
|
+
.replaceAll('// ${onSuccess}', pluginConfig.dom.onsuccess)
|
|
72
|
+
return {
|
|
73
|
+
path: 'sw-dom.js',
|
|
74
|
+
data: template,
|
|
75
|
+
layout: 'js'
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
}
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
/** 遍历指定目录下的所有文件 */
|
|
@@ -117,14 +137,11 @@ const buildNewJson = path => {
|
|
|
117
137
|
const getJsonFromNetwork = async path => {
|
|
118
138
|
const url = root + path
|
|
119
139
|
try {
|
|
120
|
-
const result = await
|
|
121
|
-
responseEncoding: 'utf-8',
|
|
122
|
-
headers: pluginConfig.headers
|
|
123
|
-
})
|
|
140
|
+
const result = await fetch(url)
|
|
124
141
|
if (result.status < 200 || result.status >= 400 || !result.data)
|
|
125
142
|
// noinspection ExceptionCaughtLocallyJS
|
|
126
143
|
throw `拉取 ${url} 时出现异常(${result.status})`
|
|
127
|
-
return result.
|
|
144
|
+
return await result.json()
|
|
128
145
|
} catch (e) {
|
|
129
146
|
if (e.toString().includes('404'))
|
|
130
147
|
logger.error(`拉取 ${url} 时出现 404,如果您是第一次构建请忽略这个错误`)
|
|
@@ -197,7 +214,7 @@ const buildUpdateJson = (name, dif, oldUpdate) => {
|
|
|
197
214
|
// 读取拓展 json
|
|
198
215
|
const expand = fs.existsSync(name) ? JSON.parse(fs.readFileSync(name, 'utf-8')) : undefined
|
|
199
216
|
// 获取上次最新的版本
|
|
200
|
-
let oldVersion = oldUpdate?.info
|
|
217
|
+
let oldVersion = oldUpdate?.info?.at(0)?.version ?? 0
|
|
201
218
|
if (typeof oldVersion !== 'number') {
|
|
202
219
|
// 当上次最新的版本号不是数字是尝试对其进行转换,如果无法转换则直接置零
|
|
203
220
|
if (oldVersion.match('\D')) oldVersion = 0
|
package/package.json
CHANGED