hexo-swpp 2.5.2 → 2.6.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.
package/index.js CHANGED
@@ -6,13 +6,41 @@ const config = hexo.config
6
6
  const pluginConfig = config.swpp || hexo.theme.config
7
7
 
8
8
  if (pluginConfig?.enable) {
9
- const nodePath = require('path')
10
- const rules = require(nodePath.resolve('./', 'sw-rules'))
11
-
9
+ const rules = loadRules()
12
10
  // 排序
13
11
  require('./lib/sort.js')(pluginConfig)
14
12
  // 生成 update.json
15
13
  require('./lib/jsonBuilder.js')(hexo, config, pluginConfig, rules)
16
14
  // 生成 sw.js
17
15
  require('./lib/swBuilder.js')(hexo, config, pluginConfig, rules)
16
+ }
17
+
18
+ // 加载 sw-rules.js 文件
19
+ function loadRules() {
20
+ const logger = require('hexo-log')()
21
+ const nodePath = require('path')
22
+ const fs = require('fs')
23
+ const themeName = hexo.config.theme
24
+ // 根目录下的文件
25
+ const root = nodePath.resolve('./', 'sw-rules')
26
+ // themes 文件夹下的文件
27
+ const themes = nodePath.resolve('./themes/', themeName)
28
+ // node_modules 文件下的文件
29
+ const modules = nodePath.resolve('./node_modules/', `hexo-theme-${themeName}`)
30
+ const exists = {
31
+ root: fs.existsSync(root),
32
+ themes: fs.existsSync(themes),
33
+ modules: fs.existsSync(modules)
34
+ }
35
+ if (!(exists.root || exists.themes || exists.modules)) {
36
+ const tip = "未找到 sw-rules.js 文件"
37
+ logger.error(tip)
38
+ throw tip
39
+ }
40
+ let result = {}
41
+ if (exists.themes)
42
+ result = require(themes)
43
+ else if (exists.modules)
44
+ result = require(modules)
45
+ return exists.root ? { ...result, ...require(root) } : result
18
46
  }
@@ -27,12 +27,16 @@
27
27
  })
28
28
  })
29
29
 
30
+ // sw 激活后立即对所有页面生效,而非等待刷新
31
+ // noinspection JSUnresolvedReference
32
+ self.addEventListener('activate', event => event.waitUntil(clients.claim()))
33
+
30
34
  // noinspection JSFileReferences
31
35
  const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')
32
36
 
33
37
  // 检查请求是否成功
34
38
  // noinspection JSUnusedLocalSymbols
35
- const checkResponse = response => response.ok || [301, 302, 307].includes(response.status)
39
+ const checkResponse = response => response.ok || [301, 302, 307, 308].includes(response.status)
36
40
 
37
41
  /**
38
42
  * 删除指定缓存
@@ -56,7 +60,7 @@
56
60
 
57
61
  self.addEventListener('fetch', event => {
58
62
  let request = event.request
59
- if (request.method !== 'GET') return
63
+ if (request.method !== 'GET' || request.url.startsWith('http')) return
60
64
  // [modifyRequest call]
61
65
  const url = new URL(request.url)
62
66
  // [blockRequest call]
@@ -64,7 +68,7 @@
64
68
  const key = `${url.protocol}//${url.host}${url.pathname}`
65
69
  event.respondWith(caches.match(key).then(cache =>
66
70
  cache ? cache : fetchFile(request, true).then(response => {
67
- if (response.status === 200) {
71
+ if (checkResponse(response)) {
68
72
  const clone = response.clone()
69
73
  caches.open(CACHE_NAME).then(it => it.put(key, clone))
70
74
  // [debug put]
package/lib/swBuilder.js CHANGED
@@ -19,8 +19,10 @@ module.exports = (hexo, config, pluginConfig, rules) => {
19
19
  const rootPath = nodePath.resolve('./')
20
20
  const relativePath = nodePath.relative(rootPath, absPath)
21
21
  // 获取拓展文件
22
- let cache = fs.readFileSync('sw-rules.js', 'utf8')
23
- .replaceAll('module.exports.', 'const ')
22
+ let cache = JSON.stringify(
23
+ rules,
24
+ (_, value) => typeof value === 'function' ? value.toString() : value
25
+ )
24
26
  if (!fetchNoCache) {
25
27
  // noinspection JSUnresolvedVariable
26
28
  if (pluginConfig.sw.cdnRacing && getCdnList) {
@@ -141,16 +143,15 @@ module.exports = (hexo, config, pluginConfig, rules) => {
141
143
  // 生成注册 sw 的代码
142
144
  // noinspection JSUnresolvedVariable
143
145
  hexo.extend.injector.register('head_begin', () =>
144
- `<script>
145
- (() => {
146
- const sw = navigator.serviceWorker
147
- const error = () => ${pluginConfig.sw.onerror}
148
- if (!sw?.register('${new URL(root).pathname}sw.js')?.then(() => {
149
- if (!sw.controller) ${pluginConfig.sw.onsuccess}
150
- })?.catch(error)) error()
151
- })()
152
- </script>`,
153
- "default")
146
+ pluginConfig.sw?.register ?? `<script>
147
+ (() => {
148
+ const sw = navigator.serviceWorker
149
+ const error = () => ${pluginConfig.sw.onerror}
150
+ if (!sw?.register('${new URL(root).pathname}sw.js')
151
+ ${pluginConfig.sw?.onsuccess ? '?.then(() => ' + pluginConfig.sw.onsuccess + ')' : ''}
152
+ ?.catch(error)) error()
153
+ })()
154
+ </script>`, "default")
154
155
 
155
156
  // 插入 sw-dom.js
156
157
  if (!pluginConfig.dom?.custom) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-swpp",
3
- "version": "2.5.2",
3
+ "version": "2.6.0-beta.0",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "hexo-log": "^3.0.0",