hexo-swpp 1.7.2 → 2.0.0-beta.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 CHANGED
@@ -16,7 +16,7 @@ const config = hexo.config
16
16
  const pluginConfig = config.swpp || hexo.theme.config
17
17
  const root = config.url + (config.root ?? '/')
18
18
  const domain = new URL(root).hostname
19
- const { cacheList, modifyRequest } = pluginConfig?.enable ? require(findScript()) : undefined
19
+ const { cacheList, modifyRequest, fetchNoCache, getUrlList } = pluginConfig?.enable ? require(findScript()) : undefined
20
20
 
21
21
  if (pluginConfig?.enable) {
22
22
  // 生成 update.json
@@ -38,11 +38,42 @@ if (pluginConfig?.enable) {
38
38
  const absPath = module.path + '/sw-template.js'
39
39
  const rootPath = nodePath.resolve('./')
40
40
  const relativePath = nodePath.relative(rootPath, absPath)
41
- const cache = fs.readFileSync('sw-cache.js', 'utf8')
41
+ // 获取拓展文件
42
+ let cache = fs.readFileSync('sw-cache.js', 'utf8')
42
43
  .replaceAll('module.exports.cacheList', 'const cacheList')
43
44
  .replaceAll('module.exports.modifyRequest', 'const modifyRequest')
45
+ if (!fetchNoCache) {
46
+ if (pluginConfig.sw.cdnRacing && getUrlList) {
47
+ cache +=`
48
+ const fetchNoCache = request => {
49
+ // noinspection JSUnresolvedFunction
50
+ const list = getUrlList(request.url)
51
+ if (!list) return fetch(request, {cache: "no-store"})
52
+ const res = list.map(url => {
53
+ const cpy = request.clone()
54
+ cpy.url = url
55
+ return cpy
56
+ })
57
+ res.push(request)
58
+ const controller = new AbortController()
59
+ return Promise.any(res.map(
60
+ it => fetch(it, {
61
+ cache: "no-store",
62
+ signal: controller.signal
63
+ }).then(response => {
64
+ if (response.status === 200 || response.status === 301 || response.status === 302) {
65
+ controller.abort()
66
+ return response
67
+ }
68
+ return Promise.reject()
69
+ })
70
+ ))
71
+ }
72
+ `
73
+ } else cache += '\nconst fetchNoCache = request => fetch(request, {cache: "no-store"})'
74
+ }
44
75
  const swContent = fs.readFileSync(relativePath, 'utf8')
45
- .replaceAll("const { cacheList, modifyRequest } = require('../sw-cache')", cache)
76
+ .replaceAll("const { cacheList, modifyRequest, fetchNoCache } = require('../sw-cache')", cache)
46
77
  .replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
47
78
  .replaceAll("'@$$[cacheName]'", `'${pluginConfig.sw.cacheName ?? 'kmarBlogCache'}'`)
48
79
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.7.2",
3
+ "version": "2.0.0-beta.1",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",
package/sw-template.js CHANGED
@@ -9,7 +9,7 @@
9
9
  self.addEventListener('install', () => self.skipWaiting())
10
10
 
11
11
  // noinspection JSFileReferences
12
- const { cacheList, modifyRequest } = require('../sw-cache')
12
+ const { cacheList, modifyRequest, fetchNoCache } = require('../sw-cache')
13
13
 
14
14
  /**
15
15
  * 删除指定缓存
@@ -60,9 +60,6 @@
60
60
  }
61
61
  })
62
62
 
63
- /** 忽略浏览器HTTP缓存的请求指定request */
64
- const fetchNoCache = request => fetch(request, {cache: "no-store"})
65
-
66
63
  /** 判断指定url击中了哪一种缓存,都没有击中则返回null */
67
64
  function findCache(url) {
68
65
  for (let key in cacheList) {