hexo-swpp 2.0.3 → 2.1.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.
- package/index.js +1 -0
- package/package.json +1 -1
- package/sw-template.js +10 -9
package/index.js
CHANGED
|
@@ -104,6 +104,7 @@ if (pluginConfig?.enable) {
|
|
|
104
104
|
} else cache += '\nconst fetchFile = (request, banCache) => fetch(request, {cache: banCache ? "no-store" : "default"})'
|
|
105
105
|
}
|
|
106
106
|
if (!modifyRequest) cache += '\nconst modifyRequest = _ => {}'
|
|
107
|
+
if (!getSpareUrls) cache += `\nconst getSpareUrls = _ => {}`
|
|
107
108
|
const swContent = fs.readFileSync(relativePath, 'utf8')
|
|
108
109
|
.replaceAll("const { cacheList, modifyRequest, fetchFile, getSpareUrls } = require('../sw-rules')", cache)
|
|
109
110
|
.replaceAll("'@$$[escape]'", (pluginConfig.sw.escape ?? 0).toString())
|
package/package.json
CHANGED
package/sw-template.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
(() => {
|
|
4
4
|
/** 缓存库名称 */
|
|
5
5
|
const CACHE_NAME = '@$$[cacheName]'
|
|
6
|
-
/**
|
|
7
|
-
const
|
|
6
|
+
/** 控制信息存储地址(必须以`/`结尾) */
|
|
7
|
+
const CTRL_PATH = 'https://id.v3/'
|
|
8
8
|
|
|
9
9
|
self.addEventListener('install', () => self.skipWaiting())
|
|
10
10
|
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
keys.map(async it => {
|
|
22
22
|
const url = it.url
|
|
23
23
|
// noinspection ES6MissingAwait,CommaExpressionJS
|
|
24
|
-
return url !==
|
|
24
|
+
return url !== CTRL_PATH && list.match(url) ? (cache.delete(it), url) : null
|
|
25
25
|
})
|
|
26
26
|
)).then(list => list.filter(it => it))
|
|
27
27
|
)
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
self.addEventListener('fetch', event => {
|
|
30
30
|
let request = event.request
|
|
31
31
|
if (request.method !== 'GET') return
|
|
32
|
-
const
|
|
32
|
+
const modify = modifyRequest(request)
|
|
33
|
+
const newRequest = modify ?? request
|
|
33
34
|
const url = new URL(newRequest.url)
|
|
34
35
|
if (findCache(url)) {
|
|
35
36
|
const key = `${url.protocol}//${url.host}${url.pathname}`
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
} else {
|
|
46
47
|
const spare = getSpareUrls(newRequest.url)
|
|
47
48
|
if (spare) event.respondWith(fetchFile(newRequest, false, spare))
|
|
48
|
-
else if (
|
|
49
|
+
else if (modify) event.respondWith(fetch(newRequest))
|
|
49
50
|
}
|
|
50
51
|
})
|
|
51
52
|
|
|
@@ -94,17 +95,17 @@
|
|
|
94
95
|
}
|
|
95
96
|
/** 解析字符串 */
|
|
96
97
|
const parseJson = json => {
|
|
97
|
-
/**
|
|
98
|
+
/** 控制信息读写操作 */
|
|
98
99
|
const dbVersion = {
|
|
99
100
|
write: (id) => caches.open(CACHE_NAME)
|
|
100
|
-
.then(cache => cache.put(
|
|
101
|
-
read: () => caches.match(
|
|
101
|
+
.then(cache => cache.put(CTRL_PATH, new Response(JSON.stringify(id)))),
|
|
102
|
+
read: () => caches.match(CTRL_PATH).then(response => response?.json())
|
|
102
103
|
}
|
|
103
104
|
let list = new VersionList()
|
|
104
105
|
return dbVersion.read().then(oldVersion => {
|
|
105
106
|
const {info, global} = json
|
|
106
107
|
const escape = '@$$[escape]'
|
|
107
|
-
const newVersion = {global
|
|
108
|
+
const newVersion = {global, local: info[0].version, escape}
|
|
108
109
|
//新用户不进行更新操作
|
|
109
110
|
if (!oldVersion) {
|
|
110
111
|
dbVersion.write(newVersion)
|