hexo-swpp 2.2.0 → 2.2.2

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.
Files changed (3) hide show
  1. package/index.js +33 -13
  2. package/package.json +1 -1
  3. package/sw-template.js +5 -2
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, {cache: banCache ? 'no-store' : 'default'})
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
- cache: "no-store",
96
- signal: (controllers[index] = new AbortController()).signal
97
- }).then(response => response.status < 303 ? {index, response} : Promise.reject())
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, {cache: banCache ? 'no-store' : 'default'})
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.status < 303) {
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 += '\nconst fetchFile = (request, banCache) => fetch(request, {cache: banCache ? "no-store" : "default"})'
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 整理分类,并将 expand 整合到结果中
586
+ // 将 diff 整理分类
567
587
  const tidyDiff = (dif, expand) => {
568
588
  const tidied = {
569
589
  /** 所有 HTML 页面 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
package/sw-template.js CHANGED
@@ -11,6 +11,9 @@
11
11
  // noinspection JSFileReferences
12
12
  const { cacheList, modifyRequest, fetchFile, getSpareUrls } = require('../sw-rules')
13
13
 
14
+ // 检查请求是否成功
15
+ const checkResponse = response => response.ok || [301, 302, 307].includes(response.status)
16
+
14
17
  /**
15
18
  * 删除指定缓存
16
19
  * @param list 要删除的缓存列表
@@ -36,7 +39,7 @@
36
39
  const key = `${url.protocol}//${url.host}${url.pathname}`
37
40
  event.respondWith(caches.match(key).then(cache =>
38
41
  cache ? cache : fetchFile(newRequest, true).then(response => {
39
- if (response.status < 303) {
42
+ if (checkResponse(response)) {
40
43
  const clone = response.clone()
41
44
  caches.open(CACHE_NAME).then(it => it.put(key, clone))
42
45
  }
@@ -126,7 +129,7 @@
126
129
  }
127
130
  return fetchFile(new Request('/update.json'), false)
128
131
  .then(response => {
129
- if (response.ok || response.status === 301 || response.status === 302)
132
+ if (checkResponse(response))
130
133
  return response.json().then(json =>
131
134
  parseJson(json).then(result => result.list ?
132
135
  deleteCache(result.list).then(list => {