hexo-swpp 1.7.1 → 2.0.0-beta.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.
Files changed (3) hide show
  1. package/index.js +36 -11
  2. package/package.json +1 -1
  3. package/sw-template.js +1 -4
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, replaceList } = 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,9 +38,40 @@ 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
76
  .replaceAll("const { cacheList, modifyRequest } = require('../sw-cache')", cache)
46
77
  .replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
@@ -517,15 +548,9 @@ function findCache(url) {
517
548
  }
518
549
 
519
550
  function replaceRequest(url) {
520
- for (let key in replaceList) {
521
- const value = replaceList[key]
522
- for (let source of value.source) {
523
- if (url.match(source)) {
524
- url = url.replace(source, value.dist)
525
- }
526
- }
527
- }
528
- return url
551
+ const request = new Request(url)
552
+ const edit = modifyRequest(request)
553
+ return edit ? request.url : url
529
554
  }
530
555
 
531
556
  function replaceDevRequest(url) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "1.7.1",
3
+ "version": "2.0.0-beta.0",
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) {