hexo-swpp 2.0.2 → 2.1.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/index.js +6 -7
- package/package.json +1 -1
- package/sw-template.js +10 -9
package/index.js
CHANGED
|
@@ -71,30 +71,28 @@ if (pluginConfig?.enable) {
|
|
|
71
71
|
} else if (pluginConfig.sw.spareUrl && getSpareUrls) {
|
|
72
72
|
cache += `
|
|
73
73
|
const fetchFile = (request, banCache, spare = null) => {
|
|
74
|
-
if (!spare)
|
|
75
|
-
spare = getSpareUrls(request.url)
|
|
74
|
+
if (!spare) spare = getSpareUrls(request.url)
|
|
76
75
|
if (!spare) return fetch(request, {cache: banCache ? 'no-store' : 'default'})
|
|
77
76
|
const list = spare.list
|
|
78
77
|
const controllers = []
|
|
79
|
-
let index = 0
|
|
80
78
|
let error = 0
|
|
81
79
|
return new Promise((resolve, reject) => {
|
|
82
80
|
const plusError = () => {
|
|
83
81
|
if (++error === list.length) reject(\`请求 \${request.url} 失败\`)
|
|
84
82
|
}
|
|
85
83
|
const pull = () => {
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
const flag = controllers.length
|
|
85
|
+
if (flag === list.length) return
|
|
88
86
|
controllers.push({
|
|
89
87
|
ctrl: new AbortController(),
|
|
90
88
|
id: setTimeout(pull, spare.timeout)
|
|
91
89
|
})
|
|
92
|
-
fetch(new Request(list[flag
|
|
90
|
+
fetch(new Request(list[flag], request)).then(response => {
|
|
93
91
|
if (response.status < 303) {
|
|
94
92
|
for (let i in controllers) {
|
|
95
93
|
if (i !== flag) controllers[i].ctrl.abort()
|
|
96
|
-
clearTimeout(controllers[i].id)
|
|
97
94
|
}
|
|
95
|
+
clearTimeout(controllers[controllers.length - 1].id)
|
|
98
96
|
resolve(response)
|
|
99
97
|
} else plusError()
|
|
100
98
|
}).catch(plusError)
|
|
@@ -106,6 +104,7 @@ if (pluginConfig?.enable) {
|
|
|
106
104
|
} else cache += '\nconst fetchFile = (request, banCache) => fetch(request, {cache: banCache ? "no-store" : "default"})'
|
|
107
105
|
}
|
|
108
106
|
if (!modifyRequest) cache += '\nconst modifyRequest = _ => {}'
|
|
107
|
+
if (!getSpareUrls) cache += `\nconst getSpareUrls = _ => {}`
|
|
109
108
|
const swContent = fs.readFileSync(relativePath, 'utf8')
|
|
110
109
|
.replaceAll("const { cacheList, modifyRequest, fetchFile, getSpareUrls } = require('../sw-rules')", cache)
|
|
111
110
|
.replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
|
package/package.json
CHANGED
package/sw-template.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
(() => {
|
|
4
4
|
/** 缓存库名称 */
|
|
5
5
|
const CACHE_NAME = '@$$[cacheName]'
|
|
6
|
-
/**
|
|
7
|
-
const
|
|
6
|
+
/** 控制信息存储地址(必须以`/`结尾) */
|
|
7
|
+
const CTRL_PATH = 'https://id.v3/'
|
|
8
8
|
|
|
9
9
|
self.addEventListener('install', () => self.skipWaiting())
|
|
10
10
|
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
keys.map(async it => {
|
|
22
22
|
const url = it.url
|
|
23
23
|
// noinspection ES6MissingAwait,CommaExpressionJS
|
|
24
|
-
return url !==
|
|
24
|
+
return url !== CTRL_PATH && list.match(url) ? (cache.delete(it), url) : null
|
|
25
25
|
})
|
|
26
26
|
)).then(list => list.filter(it => it))
|
|
27
27
|
)
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
self.addEventListener('fetch', event => {
|
|
30
30
|
let request = event.request
|
|
31
31
|
if (request.method !== 'GET') return
|
|
32
|
-
const
|
|
32
|
+
const modify = modifyRequest(request)
|
|
33
|
+
const newRequest = modify ?? request
|
|
33
34
|
const url = new URL(newRequest.url)
|
|
34
35
|
if (findCache(url)) {
|
|
35
36
|
const key = `${url.protocol}//${url.host}${url.pathname}`
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
} else {
|
|
46
47
|
const spare = getSpareUrls(newRequest.url)
|
|
47
48
|
if (spare) event.respondWith(fetchFile(newRequest, false, spare))
|
|
48
|
-
else if (
|
|
49
|
+
else if (modify) event.respondWith(fetch(newRequest))
|
|
49
50
|
}
|
|
50
51
|
})
|
|
51
52
|
|
|
@@ -94,17 +95,17 @@
|
|
|
94
95
|
}
|
|
95
96
|
/** 解析字符串 */
|
|
96
97
|
const parseJson = json => {
|
|
97
|
-
/**
|
|
98
|
+
/** 控制信息读写操作 */
|
|
98
99
|
const dbVersion = {
|
|
99
100
|
write: (id) => caches.open(CACHE_NAME)
|
|
100
|
-
.then(cache => cache.put(
|
|
101
|
-
read: () => caches.match(
|
|
101
|
+
.then(cache => cache.put(CTRL_PATH, new Response(JSON.stringify(id)))),
|
|
102
|
+
read: () => caches.match(CTRL_PATH).then(response => response?.json())
|
|
102
103
|
}
|
|
103
104
|
let list = new VersionList()
|
|
104
105
|
return dbVersion.read().then(oldVersion => {
|
|
105
106
|
const {info, global} = json
|
|
106
107
|
const escape = '@$$[escape]'
|
|
107
|
-
const newVersion = {global
|
|
108
|
+
const newVersion = {global, local: info[0].version, escape}
|
|
108
109
|
//新用户不进行更新操作
|
|
109
110
|
if (!oldVersion) {
|
|
110
111
|
dbVersion.write(newVersion)
|