hexo-swpp 2.2.1 → 2.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 +33 -13
- package/package.json +1 -1
- package/sw-template.js +13 -15
package/index.js
CHANGED
|
@@ -86,15 +86,20 @@ if (pluginConfig?.enable) {
|
|
|
86
86
|
if (pluginConfig.sw.cdnRacing && getCdnList) {
|
|
87
87
|
cache +=`
|
|
88
88
|
const fetchFile = (request, banCache) => {
|
|
89
|
+
const fetchArgs = {
|
|
90
|
+
cache: banCache ? 'no-store' : 'default',
|
|
91
|
+
mode: 'cors',
|
|
92
|
+
credentials: 'same-origin'
|
|
93
|
+
}
|
|
89
94
|
const list = getCdnList(request.url)
|
|
90
|
-
if (!list || !Promise.any) return fetch(request,
|
|
95
|
+
if (!list || !Promise.any) return fetch(request, fetchArgs)
|
|
91
96
|
const res = list.map(url => new Request(url, request))
|
|
92
97
|
const controllers = []
|
|
93
98
|
return Promise.any(res.map(
|
|
94
|
-
(it, index) => fetch(it,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
(it, index) => fetch(it, Object.assign(
|
|
100
|
+
{signal: (controllers[index] = new AbortController()).signal},
|
|
101
|
+
fetchArgs
|
|
102
|
+
)).then(response => checkResponse(response) ? {index, response} : Promise.reject())
|
|
98
103
|
)).then(it => {
|
|
99
104
|
for (let i in controllers) {
|
|
100
105
|
if (i != it.index) controllers[i].abort()
|
|
@@ -106,24 +111,33 @@ if (pluginConfig?.enable) {
|
|
|
106
111
|
} else if (pluginConfig.sw.spareUrl && getSpareUrls) {
|
|
107
112
|
cache += `
|
|
108
113
|
const fetchFile = (request, banCache, spare = null) => {
|
|
114
|
+
const fetchArgs = {
|
|
115
|
+
cache: banCache ? 'no-store' : 'default',
|
|
116
|
+
mode: 'cors',
|
|
117
|
+
credentials: 'same-origin'
|
|
118
|
+
}
|
|
109
119
|
if (!spare) spare = getSpareUrls(request.url)
|
|
110
|
-
if (!spare) return fetch(request,
|
|
120
|
+
if (!spare) return fetch(request, fetchArgs)
|
|
111
121
|
const list = spare.list
|
|
112
122
|
const controllers = []
|
|
113
123
|
let error = 0
|
|
114
124
|
return new Promise((resolve, reject) => {
|
|
115
|
-
const plusError = () => {
|
|
116
|
-
if (++error === list.length) reject(\`请求 \${request.url} 失败\`)
|
|
117
|
-
}
|
|
118
125
|
const pull = () => {
|
|
119
126
|
const flag = controllers.length
|
|
120
127
|
if (flag === list.length) return
|
|
128
|
+
const plusError = () => {
|
|
129
|
+
if (++error === list.length) reject(\`请求 \${request.url} 失败\`)
|
|
130
|
+
else if (flag + 1 === controllers.length) {
|
|
131
|
+
clearTimeout(controllers[flag].id)
|
|
132
|
+
pull()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
121
135
|
controllers.push({
|
|
122
136
|
ctrl: new AbortController(),
|
|
123
137
|
id: setTimeout(pull, spare.timeout)
|
|
124
138
|
})
|
|
125
|
-
fetch(new Request(list[flag], request)).then(response => {
|
|
126
|
-
if (response
|
|
139
|
+
fetch(new Request(list[flag], request), fetchArgs).then(response => {
|
|
140
|
+
if (checkResponse(response)) {
|
|
127
141
|
for (let i in controllers) {
|
|
128
142
|
if (i !== flag) controllers[i].ctrl.abort()
|
|
129
143
|
}
|
|
@@ -136,7 +150,13 @@ if (pluginConfig?.enable) {
|
|
|
136
150
|
})
|
|
137
151
|
}
|
|
138
152
|
`
|
|
139
|
-
} else cache +=
|
|
153
|
+
} else cache += `
|
|
154
|
+
const fetchFile = (request, banCache) => fetch(request, {
|
|
155
|
+
cache: banCache ? "no-store" : "default",
|
|
156
|
+
mode: 'cors',
|
|
157
|
+
credentials: 'same-origin'
|
|
158
|
+
})
|
|
159
|
+
`
|
|
140
160
|
}
|
|
141
161
|
if (!modifyRequest) cache += '\nconst modifyRequest = _ => {}'
|
|
142
162
|
if (!getSpareUrls) cache += `\nconst getSpareUrls = _ => {}`
|
|
@@ -563,7 +583,7 @@ const pushUpdateToInfo = (info, tidied) => {
|
|
|
563
583
|
}
|
|
564
584
|
}
|
|
565
585
|
|
|
566
|
-
// 将 diff
|
|
586
|
+
// 将 diff 整理分类
|
|
567
587
|
const tidyDiff = (dif, expand) => {
|
|
568
588
|
const tidied = {
|
|
569
589
|
/** 所有 HTML 页面 */
|
package/package.json
CHANGED
package/sw-template.js
CHANGED
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
// noinspection JSFileReferences
|
|
12
12
|
const { cacheList, modifyRequest, fetchFile, getSpareUrls } = require('../sw-rules')
|
|
13
13
|
|
|
14
|
+
// 检查请求是否成功
|
|
15
|
+
// noinspection JSUnusedLocalSymbols
|
|
16
|
+
const checkResponse = response => response.ok || [301, 302, 307].includes(response.status)
|
|
17
|
+
|
|
14
18
|
/**
|
|
15
19
|
* 删除指定缓存
|
|
16
20
|
* @param list 要删除的缓存列表
|
|
@@ -36,7 +40,7 @@
|
|
|
36
40
|
const key = `${url.protocol}//${url.host}${url.pathname}`
|
|
37
41
|
event.respondWith(caches.match(key).then(cache =>
|
|
38
42
|
cache ? cache : fetchFile(newRequest, true).then(response => {
|
|
39
|
-
if (response.status
|
|
43
|
+
if (response.status === 200) {
|
|
40
44
|
const clone = response.clone()
|
|
41
45
|
caches.open(CACHE_NAME).then(it => it.put(key, clone))
|
|
42
46
|
}
|
|
@@ -125,20 +129,14 @@
|
|
|
125
129
|
})
|
|
126
130
|
}
|
|
127
131
|
return fetchFile(new Request('/update.json'), false)
|
|
128
|
-
.then(response =>
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
}) : {version: result}
|
|
138
|
-
)
|
|
139
|
-
)
|
|
140
|
-
else throw `加载 update.json 时遇到异常,状态码:${response.status}`
|
|
141
|
-
})
|
|
132
|
+
.then(response => response.json())
|
|
133
|
+
.then(json =>
|
|
134
|
+
parseJson(json).then(result =>
|
|
135
|
+
result.list ? deleteCache(result.list).then(list => {
|
|
136
|
+
return {list, version: result.version}
|
|
137
|
+
}) : {version: result}
|
|
138
|
+
)
|
|
139
|
+
)
|
|
142
140
|
}
|
|
143
141
|
|
|
144
142
|
/**
|