hexo-swpp 1.4.9 → 1.5.1
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 +11 -12
- package/package.json +1 -1
- package/sw-dom.js +1 -1
- package/sw-template.js +47 -88
package/index.js
CHANGED
|
@@ -119,23 +119,22 @@ const isSkipFetch = url => {
|
|
|
119
119
|
*/
|
|
120
120
|
const buildNewJson = path => new Promise(resolve => {
|
|
121
121
|
const result = {} // 存储新的 MD5 表
|
|
122
|
+
const removeIndex = config.pretty_urls?.trailing_index
|
|
123
|
+
const removeHtml = config.pretty_urls?.trailing_html
|
|
122
124
|
const taskList = [] // 拉取任务列表
|
|
123
125
|
const cache = new Set() // 已经计算过的文件
|
|
124
|
-
|
|
125
|
-
const start = publicDir.length + 1
|
|
126
|
-
eachAllFile(publicDir, path => {
|
|
126
|
+
eachAllFile(config.public_dir, path => {
|
|
127
127
|
if (!fs.existsSync(path)) return logger.error(`${path} 不存在!`)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (path.endsWith('
|
|
131
|
-
else
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (isExclude(prettyUrl.href) || isExclude(srcUrl.href)) return
|
|
128
|
+
let endIndex
|
|
129
|
+
if (removeIndex && path.endsWith('/index.html')) endIndex = path.length - 10
|
|
130
|
+
else if (removeHtml && path.endsWith('.html')) endIndex = path.length - 5
|
|
131
|
+
else endIndex = path.length
|
|
132
|
+
const url = new URL(nodePath.join(root, path.substring(7, endIndex)))
|
|
133
|
+
if (isExclude(url.href)) return
|
|
135
134
|
let content = null
|
|
136
|
-
if (findCache(
|
|
135
|
+
if (findCache(url)) {
|
|
137
136
|
content = fs.readFileSync(path, 'utf-8')
|
|
138
|
-
const key = decodeURIComponent(
|
|
137
|
+
const key = decodeURIComponent(url.pathname)
|
|
139
138
|
result[key] = crypto.createHash('md5').update(content).digest('hex')
|
|
140
139
|
}
|
|
141
140
|
// 外链监控
|
package/package.json
CHANGED
package/sw-dom.js
CHANGED
|
@@ -44,7 +44,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
44
44
|
if (!list) break
|
|
45
45
|
sessionStorage.setItem('updated', '1')
|
|
46
46
|
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
|
|
47
|
-
if (window.Pjax
|
|
47
|
+
if (window.Pjax?.isSupported()) {
|
|
48
48
|
Promise.all(list.map(url => {
|
|
49
49
|
if (url.endsWith('.js'))
|
|
50
50
|
return pjaxUpdate(url)
|
package/sw-template.js
CHANGED
|
@@ -15,19 +15,14 @@
|
|
|
15
15
|
* @param list 要删除的缓存列表
|
|
16
16
|
* @return {Promise<Array<string>>} 删除的缓存的URL列表
|
|
17
17
|
*/
|
|
18
|
-
const deleteCache = list =>
|
|
19
|
-
|
|
20
|
-
.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} else resolve1(undefined)
|
|
27
|
-
})
|
|
28
|
-
)).then(removeList => resolve(removeList)))
|
|
29
|
-
)
|
|
30
|
-
})
|
|
18
|
+
const deleteCache = list => caches.open(CACHE_NAME).then(cache => cache.keys()
|
|
19
|
+
.then(keys => Promise.all(
|
|
20
|
+
keys.map(async it => {
|
|
21
|
+
const url = it.url
|
|
22
|
+
return url !== VERSION_PATH && list.match(url) ? cache.delete(it) : null
|
|
23
|
+
})
|
|
24
|
+
))
|
|
25
|
+
)
|
|
31
26
|
|
|
32
27
|
self.addEventListener('fetch', event => {
|
|
33
28
|
const request = event.request
|
|
@@ -54,26 +49,15 @@
|
|
|
54
49
|
})
|
|
55
50
|
|
|
56
51
|
self.addEventListener('message', event => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
update: info.update,
|
|
65
|
-
version: info.version,
|
|
66
|
-
})
|
|
52
|
+
if (event.data === 'update') {
|
|
53
|
+
updateJson().then(info => {
|
|
54
|
+
// noinspection JSUnresolvedVariable
|
|
55
|
+
event.source.postMessage({
|
|
56
|
+
type: 'update',
|
|
57
|
+
update: info.update,
|
|
58
|
+
version: info.version,
|
|
67
59
|
})
|
|
68
|
-
|
|
69
|
-
default:
|
|
70
|
-
const list = new VersionList()
|
|
71
|
-
list.push(new CacheChangeExpression({'flag': 'all'}))
|
|
72
|
-
deleteCache(list).then(() => {
|
|
73
|
-
if (data === 'refresh')
|
|
74
|
-
event.source.postMessage({type: 'refresh'})
|
|
75
|
-
})
|
|
76
|
-
break
|
|
60
|
+
})
|
|
77
61
|
}
|
|
78
62
|
})
|
|
79
63
|
|
|
@@ -120,58 +104,39 @@
|
|
|
120
104
|
* 解析elements,并把结果输出到list中
|
|
121
105
|
* @return boolean 是否刷新全站缓存
|
|
122
106
|
*/
|
|
123
|
-
const parseChange = (list, elements,
|
|
124
|
-
let result = true
|
|
107
|
+
const parseChange = (list, elements, ver) => {
|
|
125
108
|
for (let element of elements) {
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
const jsonList = element['change']
|
|
132
|
-
if (jsonList) {
|
|
133
|
-
for (let it of jsonList)
|
|
109
|
+
const {version, change} = element
|
|
110
|
+
if (version === ver) return false
|
|
111
|
+
if (change) {
|
|
112
|
+
for (let it of change)
|
|
134
113
|
list.push(new CacheChangeExpression(it))
|
|
135
114
|
}
|
|
136
115
|
}
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
return result
|
|
116
|
+
// 跨版本幅度过大,直接清理全站
|
|
117
|
+
return true
|
|
140
118
|
}
|
|
141
119
|
/** 解析字符串 */
|
|
142
|
-
const parseJson = json =>
|
|
120
|
+
const parseJson = json => {
|
|
143
121
|
/** 版本号读写操作 */
|
|
144
122
|
const dbVersion = {
|
|
145
|
-
write: (id) =>
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
new Request(VERSION_PATH),
|
|
149
|
-
new Response(id)
|
|
150
|
-
).then(() => resolve())
|
|
151
|
-
}).catch(() => reject())
|
|
152
|
-
}), read: () => new Promise((resolve) => {
|
|
153
|
-
caches.match(new Request(VERSION_PATH))
|
|
154
|
-
.then(response => {
|
|
155
|
-
if (!response) resolve(null)
|
|
156
|
-
response.text().then(text => {
|
|
157
|
-
resolve(text)
|
|
158
|
-
})
|
|
159
|
-
}).catch(() => resolve(null)
|
|
160
|
-
)
|
|
161
|
-
})
|
|
123
|
+
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())
|
|
162
126
|
}
|
|
163
127
|
let list = new VersionList()
|
|
164
|
-
dbVersion.read().then(
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
const newVersion = {global: global, local: elementList[0].version}
|
|
128
|
+
return dbVersion.read().then(oldVersion => {
|
|
129
|
+
const {elementList, global} = json
|
|
130
|
+
const escape = '@$$[escape]'
|
|
131
|
+
const newVersion = {global: global, local: elementList[0].version, escape: escape}
|
|
169
132
|
//新用户不进行更新操作
|
|
170
133
|
if (!oldVersion) {
|
|
171
134
|
dbVersion.write(JSON.stringify(newVersion))
|
|
172
|
-
return
|
|
135
|
+
return newVersion
|
|
173
136
|
}
|
|
174
|
-
|
|
137
|
+
// noinspection JSIncompatibleTypesComparison
|
|
138
|
+
let refresh =
|
|
139
|
+
escape !== 0 && escape !== oldVersion.escape ? true : parseChange(list, elementList, oldVersion.local)
|
|
175
140
|
dbVersion.write(JSON.stringify(newVersion))
|
|
176
141
|
//如果需要清理全站
|
|
177
142
|
if (refresh) {
|
|
@@ -180,30 +145,24 @@
|
|
|
180
145
|
list.push(new CacheChangeExpression({'flag': 'all'}))
|
|
181
146
|
} else list.refresh = true
|
|
182
147
|
}
|
|
183
|
-
|
|
184
|
-
}).catch(e => {
|
|
185
|
-
console.error(e)
|
|
186
|
-
console.error('更新过程中发生异常,已经还原版本信息!')
|
|
187
|
-
dbVersion.write('{"global":-1, "local": -1}')
|
|
148
|
+
return {list: list, version: newVersion}
|
|
188
149
|
})
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return new Promise(resolve => fetchNoCache(url)
|
|
150
|
+
}
|
|
151
|
+
return fetchNoCache(`/update.json`)
|
|
192
152
|
.then(response => {
|
|
193
153
|
if (response.ok || response.status === 301 || response.status === 302)
|
|
194
|
-
response.json().then(json =>
|
|
195
|
-
parseJson(json).then(result =>
|
|
196
|
-
|
|
197
|
-
|
|
154
|
+
return response.json().then(json =>
|
|
155
|
+
parseJson(json).then(result => result.list ?
|
|
156
|
+
deleteCache(result.list).then(list => {
|
|
157
|
+
return {
|
|
198
158
|
update: list.filter(it => it),
|
|
199
159
|
version: result.version
|
|
200
|
-
}
|
|
201
|
-
)
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
else
|
|
160
|
+
}
|
|
161
|
+
}) : {version: result}
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
else throw `加载 update.json 时遇到异常,状态码:${response.status}`
|
|
205
165
|
})
|
|
206
|
-
)
|
|
207
166
|
}
|
|
208
167
|
|
|
209
168
|
/** 版本列表 */
|