swpp-backends 2.1.7 → 2.1.8
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.
|
@@ -59,7 +59,7 @@ function buildServiceWorker() {
|
|
|
59
59
|
url = new URL(request.url)
|
|
60
60
|
}
|
|
61
61
|
`).replaceAll('// [modifyRequest else-if]', `
|
|
62
|
-
else if (modify) handleFetch(
|
|
62
|
+
else if (modify) handleFetch(fetchWithCors(request, false).catch(err => new Response(err, {status: 499})))
|
|
63
63
|
`);
|
|
64
64
|
}
|
|
65
65
|
if (blockRequest) {
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const VersionAnalyzer_1 = require("./VersionAnalyzer");
|
|
|
10
10
|
const DomBuilder_1 = require("./DomBuilder");
|
|
11
11
|
// noinspection JSUnusedGlobalSymbols
|
|
12
12
|
exports.default = {
|
|
13
|
-
version: '2.1.
|
|
13
|
+
version: '2.1.8',
|
|
14
14
|
cache: {
|
|
15
15
|
readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
|
|
16
16
|
readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
|
package/dist/resources/sw-dom.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
document.addEventListener('DOMContentLoaded', () => {
|
|
2
|
-
|
|
3
|
-
const checkServiceWorker = () => 'serviceWorker' in navigator && navigator.serviceWorker.controller
|
|
2
|
+
if (!navigator.serviceWorker?.controller) return
|
|
4
3
|
/** 发送信息到 sw */
|
|
5
4
|
const postMessage2SW = type => navigator.serviceWorker.controller.postMessage(type)
|
|
6
5
|
const pjaxUpdate = url => {
|
|
@@ -19,26 +18,26 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
if (
|
|
21
|
+
const prevUpdated = sessionStorage.getItem('updated')
|
|
22
|
+
if (prevUpdated) {
|
|
24
23
|
sessionStorage.removeItem('updated');
|
|
25
24
|
// ${onSuccess}
|
|
26
|
-
} else postMessage2SW('update')
|
|
25
|
+
} else if (prevUpdated !== '2') postMessage2SW('update')
|
|
27
26
|
navigator.serviceWorker.addEventListener('message', event => {
|
|
28
27
|
const data = event.data
|
|
29
28
|
switch (data.type) {
|
|
30
29
|
case 'update':
|
|
31
|
-
const list = data.
|
|
32
|
-
if (!list) break
|
|
30
|
+
const list = data.list
|
|
33
31
|
sessionStorage.setItem('updated', '1')
|
|
34
32
|
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
|
|
35
|
-
if (window.Pjax?.isSupported()) {
|
|
33
|
+
if (list && window.Pjax?.isSupported()) {
|
|
36
34
|
list.filter(url => /\.(js|css)$/.test(url))
|
|
37
35
|
.forEach(pjaxUpdate)
|
|
38
36
|
}
|
|
39
37
|
location.reload()
|
|
40
38
|
break
|
|
41
39
|
case 'escape':
|
|
40
|
+
sessionStorage.setItem('updated', '2')
|
|
42
41
|
location.reload()
|
|
43
42
|
break
|
|
44
43
|
}
|
|
@@ -1,32 +1,21 @@
|
|
|
1
1
|
/** @type function */
|
|
2
|
-
let getRaceUrls, checkResponse, getSpareUrls
|
|
2
|
+
let getRaceUrls, checkResponse, getSpareUrls, fetchWithCors
|
|
3
3
|
|
|
4
|
-
module.exports.JS_CODE_DEF_FETCH_FILE = 'const fetchFile = '
|
|
5
|
-
(request, banCache) => fetch(request, {
|
|
6
|
-
cache: banCache ? "no-store" : "default",
|
|
7
|
-
mode: 'cors',
|
|
8
|
-
credentials: 'same-origin'
|
|
9
|
-
})
|
|
10
|
-
).toString()
|
|
4
|
+
module.exports.JS_CODE_DEF_FETCH_FILE = 'const fetchFile = fetchWithCors;'
|
|
11
5
|
|
|
12
6
|
module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
|
|
13
7
|
(request, banCache) => {
|
|
14
|
-
const fetchArgs = {
|
|
15
|
-
cache: banCache ? 'no-store' : 'default',
|
|
16
|
-
mode: 'cors',
|
|
17
|
-
credentials: 'same-origin'
|
|
18
|
-
}
|
|
19
8
|
const list = getRaceUrls(request.url)
|
|
20
|
-
if (!list || !Promise.any) return
|
|
9
|
+
if (!list || !Promise.any) return fetchWithCors(request, banCache)
|
|
21
10
|
const res = list.map(url => new Request(url, request))
|
|
22
11
|
const controllers = []
|
|
23
12
|
// noinspection JSCheckFunctionSignatures
|
|
24
|
-
return Promise.any(
|
|
25
|
-
(it, index) =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
)
|
|
13
|
+
return Promise.any(
|
|
14
|
+
res.map((it, index) => fetchWithCors(
|
|
15
|
+
it, banCache,
|
|
16
|
+
{signal: (controllers[index] = new AbortController()).signal}
|
|
17
|
+
).then(response => checkResponse(response) ? {index, response} : Promise.reject()))
|
|
18
|
+
).then(it => {
|
|
30
19
|
for (let i in controllers) {
|
|
31
20
|
if (i !== it.index) controllers[i].abort()
|
|
32
21
|
}
|
|
@@ -37,24 +26,18 @@ module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
|
|
|
37
26
|
|
|
38
27
|
module.exports.JS_CODE_GET_SPARE_URLS = 'const fetchFile = ' + (
|
|
39
28
|
(request, banCache, spare = null) => {
|
|
40
|
-
const fetchArgs = {
|
|
41
|
-
cache: banCache ? 'no-store' : 'default',
|
|
42
|
-
mode: 'cors',
|
|
43
|
-
credentials: 'same-origin'
|
|
44
|
-
}
|
|
45
29
|
if (!spare) {
|
|
46
30
|
spare = getSpareUrls(request.url)
|
|
47
|
-
if (!spare) return
|
|
31
|
+
if (!spare) return fetchWithCors(request, banCache)
|
|
48
32
|
}
|
|
49
33
|
const list = spare.list
|
|
50
34
|
const controllers = new Array(list.length)
|
|
51
35
|
// noinspection JSCheckFunctionSignatures
|
|
52
36
|
const startFetch =
|
|
53
|
-
index =>
|
|
37
|
+
index => fetchWithCors(
|
|
54
38
|
new Request(list[index], request),
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}, fetchArgs)
|
|
39
|
+
banCache,
|
|
40
|
+
{signal: (controllers[index] = new AbortController()).signal}
|
|
58
41
|
).then(response => checkResponse(response) ? {r: response, i: index} : Promise.reject())
|
|
59
42
|
return new Promise((resolve, reject) => {
|
|
60
43
|
let flag = true
|
|
@@ -92,8 +92,8 @@
|
|
|
92
92
|
}
|
|
93
93
|
cacheMap.set(cacheKey, cache = [])
|
|
94
94
|
/** 处理拉取 */
|
|
95
|
-
const handleFetch = promise =>
|
|
96
|
-
|
|
95
|
+
const handleFetch = promise => event.respondWith(
|
|
96
|
+
promise.then(response => {
|
|
97
97
|
for (let item of cache) {
|
|
98
98
|
item.s(response.clone())
|
|
99
99
|
}
|
|
@@ -104,7 +104,8 @@
|
|
|
104
104
|
}).then(() => {
|
|
105
105
|
cacheMap.delete(cacheKey)
|
|
106
106
|
return promise
|
|
107
|
-
})
|
|
107
|
+
})
|
|
108
|
+
)
|
|
108
109
|
const cacheRule = findCache(url)
|
|
109
110
|
if (cacheRule) {
|
|
110
111
|
let key = `https://${url.host}${url.pathname}`
|
|
@@ -127,7 +128,7 @@
|
|
|
127
128
|
const spare = getSpareUrls(request.url)
|
|
128
129
|
if (spare) handleFetch(fetchFile(request, false, spare))
|
|
129
130
|
// [modifyRequest else-if]
|
|
130
|
-
else handleFetch(
|
|
131
|
+
else handleFetch(fetchWithCors(request, false).catch(err => new Response(err, {status: 499})))
|
|
131
132
|
}
|
|
132
133
|
})
|
|
133
134
|
|
|
@@ -141,6 +142,22 @@
|
|
|
141
142
|
)
|
|
142
143
|
})
|
|
143
144
|
|
|
145
|
+
/**
|
|
146
|
+
* 添加 cors 配置请求指定资源
|
|
147
|
+
* @param request {Request|string}
|
|
148
|
+
* @param banCache {boolean} 是否禁用 HTTP 缓存
|
|
149
|
+
* @param optional {RequestInit?} 额外的配置项
|
|
150
|
+
* @return {Promise<Response>}
|
|
151
|
+
*/
|
|
152
|
+
const fetchWithCors = (request, banCache, optional) => fetch(
|
|
153
|
+
request,
|
|
154
|
+
Object.assign({
|
|
155
|
+
cache: optional ? "no-store" : "default",
|
|
156
|
+
mode: 'cors',
|
|
157
|
+
credentials: 'same-origin'
|
|
158
|
+
}, optional || {})
|
|
159
|
+
)
|
|
160
|
+
|
|
144
161
|
/**
|
|
145
162
|
* 判断指定 url 击中了哪一种缓存,都没有击中则返回 null
|
|
146
163
|
* @param url {URL}
|