swpp-backends 3.0.0 → 3.1.0-beta2

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/README.md CHANGED
@@ -4,55 +4,55 @@
4
4
 
5
5
  ## 什么是 swpp
6
6
 
7
-   swpp 是一个用于为网站快速生成一个高度可用的 [Service Worker](https://developer.mozilla.org/zh-CN/docs/Web/API/ServiceWorker) 的工具,旨在提高网站的可靠性、优化二次访问性能、提供离线浏览功能……
7
+ swpp(Service Worker Plus Plus)是一个现代化的 Service Worker 生成工具,专注于为静态网站提供精准、高效的缓存控制和离线访问能力。
8
8
 
9
-   常用 SW 构建工具的对比:
9
+ 与传统的 Service Worker 工具相比,swpp 提供了更细粒度的控制能力和更灵活的配置选项,让开发者能够精确管理网站资源的缓存策略。
10
10
 
11
- | | swpp@3 | swpp@2 | hexo-offline |
12
- |:----------:|:------:|:------:|:------------:|
13
- | 本地缓存 | ✔️ | ✔️ | ✔️ |
14
- | 缓存增量更新 | ✔️ | ✔️ | ❌ |
15
- | 缓存过期时间 | ✔️ | ❌ | ✔️ |
16
- | 缓存大小限制 | ❌ | ❌ | ✔️ |
17
- | 预缓存 | ❌ | ❌ | ✔️ |
18
- | Request 篡改 | ✔️ | ✔️ | ❌ |
19
- | URL 竞速 | ✔️ | ✔️ | ❌ |
20
- | 备用 URL | ✔️ | ✔️ | ❌ |
21
- | 204 阻塞响应 | ✔️ | ✔️ | ❌ |
22
- | 逃生门 | ✔️ | ✔️ | ❌ |
23
- | 请求合并 | ❌ | ✔️ | ❌ |
24
- | 高度自由 | ✔️ | ✔️ | ❌ |
25
- | 更新 | 活跃 | 停止维护 | 超过两年没有更新 |
11
+ ## 核心特性
26
12
 
27
- ## swpp v2 与 v3 的区别
13
+ ### 精准缓存控制
14
+ - **智能缓存规则匹配**:基于 URL 模式的精细化缓存策略配置
15
+ - **缓存过期时间管理**:可为不同类型的资源设置独立的过期时间
16
+ - **增量更新机制**:只更新发生变化的资源,避免全量刷新
28
17
 
29
-   swpp v3 对 swpp 的所有代码进行了完全的重构,现在代码逻辑更加的清晰。除了后台代码的差异,v3 还有以下一系列的改进:
18
+ ### 性能优化
19
+ - **URL 竞速加载**:同时请求多个 CDN 源,使用最快响应的资源
20
+ - **备用 URL 机制**:主资源加载失败时自动切换到备用地址
30
21
 
31
- - 完全定制化<br/>
32
- 现在您对 swpp 生成的 sw 拥有完全的控制权。在 v2 中,如果需要修改插件生成的 sw 的逻辑,您需要自行编写整个 sw 文件。<br/>
33
- v3 中将 sw 文件的各个部分拆分到了环境变量中,这使得您可以通过配置文件覆盖 sw 文件中的任意一个部分,或者追加任意内容。
22
+ ### 高度可定制
23
+ - **模块化配置**:将 Service Worker 功能拆分为独立的配置模块
24
+ - **TypeScript 支持**:完整的类型定义,提供更好的开发体验
25
+ - **全面开放**:允许用户修改生成的 Service Worker 文件中任意一处代码
34
26
 
35
- - 现代化配置<br/>
36
- 现在您可以使用 `ts` 语言编写配置文件,同时还可以选择使用 `ESM`。在 v2 中您必须使用 `CommonJS`。<br/>
37
- 同时使用内置的一系列 `define` 函数可以让您对配置的颗粒度进行更加完美的掌控。
27
+ ## swpp 与 hexo-offline 对比
38
28
 
39
- - 完善的类型<br/>
40
- 现在 swpp 提供了更加优雅的数据类型,以优化编写配置项、二次开发时的体验。
29
+ > hexo-offline 是基于 Workbox 构建的 Hexo 插件,采用预缓存(precaching)的工作方式,与 swpp 的设计理念存在本质区别:
30
+ >
31
+ > swpp 设计目标是在提供一系列内置的常用操作的前提下,尽可能地允许用户自定义 Service Worker 的行为,离线访问只是缓存控制产生的“副作用”;
32
+ > hexo-offline 的设计目标是实现网站的离线访问。
41
33
 
42
- ## 版本号
34
+ | 特性 | swpp | hexo-offline |
35
+ |----------|----------------------|-------------------|
36
+ | **核心技术** | 自研 Service Worker 框架 | 基于 Google Workbox |
37
+ | **配置方式** | 可编程配置,支持 TypeScript | JSON/CJS 配置文件 |
38
+ | **缓存策略** | 细粒度 URL 规则匹配 | 文件扩展名模式匹配 |
39
+ | **更新机制** | 智能增量更新 | 全量预缓存更新 |
40
+ | **性能特性** | URL 竞速、备用地址 | 基础缓存策略 |
41
+ | **定制能力** | 高度可定制,完全控制 | 零配置开箱即用 |
42
+ | **适用场景** | 复杂网站、高性能要求 | 简单博客、快速部署 |
43
43
 
44
- &emsp;&emsp;目前 swpp v3 还在测试阶段,版本号全部采用 `3.0.0-alpha.<a><b><c>` 的格式:
44
+ ### 选择建议
45
45
 
46
- - `3.0.0` - 大版本号
47
- - `alpha` - 测试版本
48
- - `a` - 一级子版本号,变动时表明 API 或配置文件等内容发生了不向前兼容的变动
49
- - `b` - 二级子版本号,变动时表明添加了新的功能
50
- - `c` - 三级子版本号,变动时表明进行了漏洞修复
46
+ 如果你:
51
47
 
52
- &emsp;&emsp;当 `a` `b` `c` 的某一位达到两位数时,会在其中添加 `x` 表示分割。
48
+ - 希望对动态资源(如 API 响应、用户内容)的缓存进行精细策略控制;
49
+ - 重视代码的透明度和可审计性,希望完全理解底层逻辑;
50
+ - 享受深入技术细节、亲手构建网络层控制逻辑的过程;
51
+ - 在个人项目或技术实验中探索 Service Worker 的潜力
53
52
 
54
- ## QQ
53
+ 那么 swpp 是一个非常值得一试的选择。
55
54
 
56
- &emsp;&emsp;注意:swpp 有且仅有下面的群是官方的:
55
+ ## 社区支持
57
56
 
58
- ![QQ 群:545028909](/qrcode.jpg)
57
+ - **GitHub Issues**:报告问题和功能请求
58
+ - **QQ 群**:545028909(官方技术支持群)
package/dist/index.d.ts CHANGED
@@ -5,7 +5,6 @@ export { utils, RuntimeException } from './swpp/untils';
5
5
  export { ResourcesScanner, FileUpdateTracker, } from './swpp/ResourcesScanner';
6
6
  export { JsonBuilder, UpdateJson, UpdateChangeExp, TrackerHeaderDiff } from './swpp/JsonBuilder';
7
7
  export { SwCompiler, CompilationData, RuntimeData, BrowserVersion } from './swpp/SwCompiler';
8
- export { NetworkFileHandler, FiniteConcurrencyFetcher } from './swpp/NetworkFileHandler';
9
8
  export { KeyValueDatabase, readThisValue } from './swpp/database/KeyValueDatabase';
10
9
  export { RuntimeKeyValueDatabase } from './swpp/database/RuntimeKeyValueDatabase';
11
10
  export { RuntimeEventCode } from './swpp/database/RuntimeEventCode';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // noinspection JSUnusedGlobalSymbols
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.defineLazyInitConfig = exports.defineNoCacheConfig = exports.defineContextConfig = exports.defineIndivisibleConfig = exports.NoCacheConfig = exports.IndivisibleConfig = exports.SpecialConfig = exports.defineModifier = exports.defineCompilationFP = exports.defineCompilationEnv = exports.defineCrossEnv = exports.defineRuntimeDep = exports.defineCrossDep = exports.defineRuntimeCore = exports.defineDomConfig = exports.defineRuntimeEvent = exports.defineConfig = exports.ConfigLoader = exports.DomCode = exports.AllowNotFoundEnum = exports.CompilationEnv = exports.CrossDepCode = exports.CrossEnv = exports.RuntimeDepCode = exports.RuntimeCoreCode = exports.RuntimeEventCode = exports.RuntimeKeyValueDatabase = exports.readThisValue = exports.KeyValueDatabase = exports.FiniteConcurrencyFetcher = exports.RuntimeData = exports.CompilationData = exports.SwCompiler = exports.JsonBuilder = exports.FileUpdateTracker = exports.ResourcesScanner = exports.RuntimeException = exports.utils = exports.BasicActions = exports.swppVersion = void 0;
4
+ exports.defineLazyInitConfig = exports.defineNoCacheConfig = exports.defineContextConfig = exports.defineIndivisibleConfig = exports.NoCacheConfig = exports.IndivisibleConfig = exports.SpecialConfig = exports.defineModifier = exports.defineCompilationFP = exports.defineCompilationEnv = exports.defineCrossEnv = exports.defineRuntimeDep = exports.defineCrossDep = exports.defineRuntimeCore = exports.defineDomConfig = exports.defineRuntimeEvent = exports.defineConfig = exports.ConfigLoader = exports.DomCode = exports.AllowNotFoundEnum = exports.CompilationEnv = exports.CrossDepCode = exports.CrossEnv = exports.RuntimeDepCode = exports.RuntimeCoreCode = exports.RuntimeEventCode = exports.RuntimeKeyValueDatabase = exports.readThisValue = exports.KeyValueDatabase = exports.RuntimeData = exports.CompilationData = exports.SwCompiler = exports.JsonBuilder = exports.FileUpdateTracker = exports.ResourcesScanner = exports.RuntimeException = exports.utils = exports.BasicActions = exports.swppVersion = void 0;
5
5
  /** 版本号 */
6
6
  exports.swppVersion = require('../package.json').version;
7
7
  var BasicActions_1 = require("./swpp/BasicActions");
@@ -18,8 +18,6 @@ var SwCompiler_1 = require("./swpp/SwCompiler");
18
18
  Object.defineProperty(exports, "SwCompiler", { enumerable: true, get: function () { return SwCompiler_1.SwCompiler; } });
19
19
  Object.defineProperty(exports, "CompilationData", { enumerable: true, get: function () { return SwCompiler_1.CompilationData; } });
20
20
  Object.defineProperty(exports, "RuntimeData", { enumerable: true, get: function () { return SwCompiler_1.RuntimeData; } });
21
- var NetworkFileHandler_1 = require("./swpp/NetworkFileHandler");
22
- Object.defineProperty(exports, "FiniteConcurrencyFetcher", { enumerable: true, get: function () { return NetworkFileHandler_1.FiniteConcurrencyFetcher; } });
23
21
  var KeyValueDatabase_1 = require("./swpp/database/KeyValueDatabase");
24
22
  Object.defineProperty(exports, "KeyValueDatabase", { enumerable: true, get: function () { return KeyValueDatabase_1.KeyValueDatabase; } });
25
23
  Object.defineProperty(exports, "readThisValue", { enumerable: true, get: function () { return KeyValueDatabase_1.readThisValue; } });
@@ -1,4 +1,3 @@
1
- import { CompilationData } from './SwCompiler';
2
1
  export declare class FilePath {
3
2
  readonly absPath: string;
4
3
  readonly baseProject: string | null;
@@ -27,10 +26,6 @@ export declare class FilePath {
27
26
  * 检查路径是否指向一个目录
28
27
  */
29
28
  isDirectory(): Promise<boolean>;
30
- /**
31
- * 检查路径是否指向一个文件
32
- */
33
- isFile(): Promise<boolean>;
34
29
  /**
35
30
  * 将指定目录拼接到当前路径之后
36
31
  */
@@ -60,12 +55,6 @@ export declare class FilePath {
60
55
  mkdirs(): Promise<void>;
61
56
  /** 空目录 */
62
57
  static EMPTY: FilePath;
63
- /**
64
- * 从绝对路径获取 FilePath
65
- * @param absPath 绝对路径
66
- * @param compilation 编译期数据
67
- */
68
- static fromAbsPath(absPath: string, compilation: CompilationData): FilePath;
69
58
  /**
70
59
  * 构建网站根目录的 FilePath
71
60
  * @param path 绝对路径或相对路径(相对于项目根目录),以 `/` 开头或 `x:/` 开头判定为绝对路径
@@ -48,14 +48,6 @@ class FilePath {
48
48
  this.statCache = await fs_1.default.promises.stat(this.absPath);
49
49
  return this.statCache.isDirectory();
50
50
  }
51
- /**
52
- * 检查路径是否指向一个文件
53
- */
54
- async isFile() {
55
- if (!this.statCache)
56
- this.statCache = await fs_1.default.promises.stat(this.absPath);
57
- return this.statCache.isFile();
58
- }
59
51
  /**
60
52
  * 将指定目录拼接到当前路径之后
61
53
  */
@@ -130,25 +122,6 @@ class FilePath {
130
122
  await fs_1.default.promises.mkdir(parent.absPath, { recursive: true });
131
123
  }
132
124
  }
133
- /**
134
- * 从绝对路径获取 FilePath
135
- * @param absPath 绝对路径
136
- * @param compilation 编译期数据
137
- */
138
- static fromAbsPath(absPath, compilation) {
139
- absPath = path_1.default.posix.normalize(absPath);
140
- const projectRoot = compilation.compilationEnv.read('PROJECT_PATH');
141
- const publicRoot = compilation.compilationEnv.read('PUBLIC_PATH');
142
- if (projectRoot.absPath.startsWith(absPath)) {
143
- return projectRoot.join(absPath.substring(projectRoot.absPath.length));
144
- }
145
- else if (publicRoot.absPath.startsWith(absPath)) {
146
- return publicRoot.join(absPath.substring(publicRoot.absPath.length));
147
- }
148
- else {
149
- return new FilePath(absPath, null, null);
150
- }
151
- }
152
125
  /**
153
126
  * 构建网站根目录的 FilePath
154
127
  * @param path 绝对路径或相对路径(相对于项目根目录),以 `/` 开头或 `x:/` 开头判定为绝对路径
@@ -10,8 +10,6 @@ export declare class ResourcesScanner {
10
10
  constructor(compilation: CompilationData, oldTracker?: FileUpdateTracker | undefined);
11
11
  /** 扫描指定目录下的所有文件 */
12
12
  scanLocalFile(path: FilePath): Promise<FileUpdateTracker>;
13
- /** 扫描网络文件 */
14
- private scanNetworkFile;
15
13
  }
16
14
  /**
17
15
  * 文件更新监听器
@@ -26,11 +24,6 @@ export declare class FileUpdateTracker {
26
24
  constructor(compilation: CompilationData, oldTracker?: FileUpdateTracker | undefined);
27
25
  /** 更新一个文件的标识符 */
28
26
  update(uri: string, value: string | Set<string> | string[]): void;
29
- /**
30
- * 同步指定的稳定资源(同步时会连同同步其连接的稳定资源)
31
- * @return 直接或间接连接的一些需要扫描的资源
32
- */
33
- syncStable(uri: URL, value: string[], oldTracker: FileUpdateTracker): string[];
34
27
  /** 读取一个文件的标识符 */
35
28
  get(uri: string): string | string[] | undefined;
36
29
  /** 归一化 uri */
@@ -79,46 +79,8 @@ class ResourcesScanner {
79
79
  }, isCached);
80
80
  set.forEach(it => urls.add(it));
81
81
  });
82
- await this.scanNetworkFile(tracker, urls);
83
82
  return tracker;
84
83
  }
85
- /** 扫描网络文件 */
86
- async scanNetworkFile(tracker, urls, record = new Set()) {
87
- const matchCacheRule = this.compilation.crossDep.read('matchCacheRule');
88
- const registry = this.compilation.fileParser;
89
- const isStable = this.compilation.compilationEnv.read('isStable');
90
- const appendedUrls = new Set();
91
- const taskList = new Array(urls.size);
92
- let i = 0;
93
- for (let url of urls) {
94
- const normalizeUri = tracker.normalizeUri(url);
95
- if (record.has(normalizeUri.href))
96
- continue;
97
- record.add(normalizeUri.href);
98
- const isCached = matchCacheRule.runOnNode(normalizeUri);
99
- if (isCached) {
100
- tracker.addUrl(normalizeUri.href);
101
- }
102
- if (isStable(normalizeUri)) {
103
- const oldValue = this.oldTracker?.get?.(normalizeUri.href);
104
- if (Array.isArray(oldValue)) {
105
- const list = tracker.syncStable(normalizeUri, oldValue, this.oldTracker);
106
- list.forEach(it => appendedUrls.add(it));
107
- continue;
108
- }
109
- }
110
- taskList[i++] = registry.parserUrlFile(normalizeUri.href, !!isCached)
111
- .then(value => {
112
- if (isCached) {
113
- tracker.update(value.file, value.mark);
114
- }
115
- value.urls.forEach(it => appendedUrls.add(it));
116
- }).catch(err => untils_1.utils.printError('SCAN NETWORK FILE', err));
117
- }
118
- await Promise.all(taskList);
119
- if (appendedUrls.size !== 0)
120
- await this.scanNetworkFile(tracker, appendedUrls, record);
121
- }
122
84
  }
123
85
  exports.ResourcesScanner = ResourcesScanner;
124
86
  /**
@@ -148,30 +110,6 @@ class FileUpdateTracker {
148
110
  this.map.set(uri, JSON.stringify(Array.from(value)));
149
111
  }
150
112
  }
151
- /**
152
- * 同步指定的稳定资源(同步时会连同同步其连接的稳定资源)
153
- * @return 直接或间接连接的一些需要扫描的资源
154
- */
155
- syncStable(uri, value, oldTracker) {
156
- const isStable = this.compilation.compilationEnv.read('isStable');
157
- this.update(uri.href, value);
158
- this.addUrl(uri.href);
159
- const result = [];
160
- for (let item of value) {
161
- this.addUrl(item);
162
- const itemUrl = new URL(item);
163
- if (isStable(itemUrl)) {
164
- const oldValue = oldTracker.get(item);
165
- if (Array.isArray(oldValue)) {
166
- const son = this.syncStable(itemUrl, oldValue, oldTracker);
167
- result.push(...son);
168
- continue;
169
- }
170
- }
171
- result.push(item);
172
- }
173
- return result;
174
- }
175
113
  /** 读取一个文件的标识符 */
176
114
  get(uri) {
177
115
  const value = this.map.get(this.normalizeUri(uri).href);
@@ -287,7 +225,7 @@ class FileUpdateTracker {
287
225
  let error;
288
226
  const result = await (async () => {
289
227
  try {
290
- const response = await fetcher.fetch(url);
228
+ const response = await fetcher(url);
291
229
  if (isNotFound.response(response)) {
292
230
  if (notFoundLevel == CompilationEnv_1.AllowNotFoundEnum.REJECT_ALL) {
293
231
  error = new untils_1.RuntimeException(untils_1.exceptionNames.notFound, `拉取 ${url} 时出现 404 错误`);
@@ -1,6 +1,5 @@
1
1
  import { FilePath } from '../FilePath';
2
2
  import { UpdateJson } from '../JsonBuilder';
3
- import { FiniteConcurrencyFetcher } from '../NetworkFileHandler';
4
3
  import { FileUpdateTracker } from '../ResourcesScanner';
5
4
  import { CompilationData } from '../SwCompiler';
6
5
  import { KeyValueDatabase } from './KeyValueDatabase';
@@ -62,7 +61,7 @@ declare function buildCommon(): {
62
61
  /**
63
62
  * 拉取网络文件
64
63
  */
65
- readonly NETWORK_FILE_FETCHER: import("./KeyValueDatabase").DatabaseValue<FiniteConcurrencyFetcher>;
64
+ readonly NETWORK_FILE_FETCHER: import("./KeyValueDatabase").DatabaseValue<(url: string | URL) => Promise<Response>>;
66
65
  /**
67
66
  * 判断文件是否是 404
68
67
  */
@@ -74,9 +73,5 @@ declare function buildCommon(): {
74
73
  * 是否允许 404
75
74
  */
76
75
  readonly ALLOW_NOT_FOUND: import("./KeyValueDatabase").DatabaseValue<AllowNotFoundEnum>;
77
- /**
78
- * 检查一个链接是否是稳定的(也就是 URL 不变其返回的结果永远不变)
79
- */
80
- readonly isStable: import("./KeyValueDatabase").DatabaseValue<(_url: URL) => boolean>;
81
76
  };
82
77
  export {};
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AllowNotFoundEnum = exports.CompilationEnv = void 0;
4
4
  const FilePath_1 = require("../FilePath");
5
- const NetworkFileHandler_1 = require("../NetworkFileHandler");
6
5
  const ResourcesScanner_1 = require("../ResourcesScanner");
7
6
  const untils_1 = require("../untils");
8
7
  const KeyValueDatabase_1 = require("./KeyValueDatabase");
@@ -117,7 +116,7 @@ function buildCommon() {
117
116
  try {
118
117
  const swppPath = (0, KeyValueDatabase_1.readThisValue)(this, 'swppPath');
119
118
  const versionPath = (0, KeyValueDatabase_1.readThisValue)(this, 'versionPath');
120
- const response = await fetcher.fetch(untils_1.utils.splicingUrl(baseUrl, swppPath, versionPath));
119
+ const response = await fetcher(untils_1.utils.splicingUrl(baseUrl, swppPath, versionPath));
121
120
  if (!isNotFound.response(response)) {
122
121
  if (isFetchSuccessful(response)) {
123
122
  const json = await response.json().catch(err => {
@@ -159,7 +158,7 @@ function buildCommon() {
159
158
  * 拉取网络文件
160
159
  */
161
160
  NETWORK_FILE_FETCHER: (0, KeyValueDatabase_1.buildEnv)({
162
- default: new NetworkFileHandler_1.FiniteConcurrencyFetcher()
161
+ default: (url) => fetch(url)
163
162
  }),
164
163
  /**
165
164
  * 判断文件是否是 404
@@ -186,11 +185,5 @@ function buildCommon() {
186
185
  }
187
186
  }
188
187
  }),
189
- /**
190
- * 检查一个链接是否是稳定的(也就是 URL 不变其返回的结果永远不变)
191
- */
192
- isStable: (0, KeyValueDatabase_1.buildEnv)({
193
- default: (_url) => false
194
- })
195
188
  };
196
189
  }
@@ -7,14 +7,6 @@ export declare class CompilationFileParser extends KeyValueDatabase<FileParser<c
7
7
  constructor();
8
8
  /** 解析本地文件 */
9
9
  parserLocalFile(path: FilePath, cb?: (content: crypto.BinaryLike) => void, force?: boolean): Promise<Set<string>>;
10
- /** 解析网络文件 */
11
- parserNetworkFile(response: Response, callback?: (content: crypto.BinaryLike) => Promise<void> | void): Promise<Set<string>>;
12
- /**
13
- * 解析指定的 URL
14
- * @param url 链接
15
- * @param isCached 该链接指向的资源是否需要缓存
16
- */
17
- parserUrlFile(url: string, isCached: boolean): Promise<FileMark>;
18
10
  /**
19
11
  * 解析指定类型的文件内容
20
12
  * @param type 文件类型
@@ -60,69 +60,6 @@ class CompilationFileParser extends KeyValueDatabase_1.KeyValueDatabase {
60
60
  return new Set();
61
61
  }
62
62
  }
63
- /** 解析网络文件 */
64
- async parserNetworkFile(response, callback) {
65
- const url = response.url;
66
- const fileHandler = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
67
- const contentType = fileHandler.getUrlContentType(url, response);
68
- if (this.hasKey(contentType)) {
69
- const parser = this.read(contentType);
70
- const content = await parser.readFromNetwork(this.compilation, response);
71
- if (callback)
72
- await callback(content);
73
- return await parser.extractUrls(this.compilation, content, new URL(url));
74
- }
75
- else {
76
- if (callback) {
77
- const buffer = await response.arrayBuffer();
78
- const array = new Uint8Array(buffer);
79
- callback(array);
80
- }
81
- return new Set();
82
- }
83
- }
84
- /**
85
- * 解析指定的 URL
86
- * @param url 链接
87
- * @param isCached 该链接指向的资源是否需要缓存
88
- */
89
- async parserUrlFile(url, isCached) {
90
- const fileHandler = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
91
- const contentType = fileHandler.getUrlContentType(url);
92
- if (!contentType && !isCached)
93
- return { file: url, mark: '', urls: new Set() };
94
- const parser = this.hasKey(contentType) ? this.read(contentType) : undefined;
95
- if (!parser && !isCached)
96
- return { file: url, mark: '', urls: new Set() };
97
- if (parser?.calcUrl) {
98
- const result = await parser.calcUrl(url);
99
- if (result)
100
- return {
101
- file: url,
102
- ...result
103
- };
104
- }
105
- const fetcher = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
106
- const urls = new Set();
107
- let mark = '';
108
- await fetcher.fetch(url)
109
- .then(response => this.parserNetworkFile(response, isCached ? content => {
110
- mark = untils_1.utils.calcHash(content);
111
- } : undefined))
112
- .then(urls => urls.forEach(it => urls.add(it)))
113
- .catch(err => new Response(JSON.stringify({
114
- type: err.type,
115
- message: err.message,
116
- stack: err.stack,
117
- addition: err
118
- }), {
119
- status: 599,
120
- headers: {
121
- 'Content-Type': 'application/json'
122
- }
123
- }));
124
- return { file: url, mark, urls };
125
- }
126
63
  /**
127
64
  * 解析指定类型的文件内容
128
65
  * @param type 文件类型
@@ -9,6 +9,8 @@ export declare class CrossEnv extends RuntimeKeyValueDatabase<any, COMMON_TYPE_C
9
9
  declare function buildCommon(): {
10
10
  /** 缓存库名称 */
11
11
  readonly CACHE_NAME: import("./KeyValueDatabase").DatabaseValue<string>;
12
+ /** 网站的基准域名 */
13
+ readonly BASE_URL: import("./KeyValueDatabase").DatabaseValue<string>;
12
14
  /** 永久缓存标记 */
13
15
  readonly INFINITE_CACHE: import("./KeyValueDatabase").DatabaseValue<symbol>;
14
16
  /** 存储版本号的 URL */
@@ -32,6 +32,18 @@ function buildCommon() {
32
32
  return {
33
33
  /** 缓存库名称 */
34
34
  CACHE_NAME: (0, KeyValueDatabase_1.buildEnv)({ default: 'kmarBlogCache' }),
35
+ /** 网站的基准域名 */
36
+ BASE_URL: (0, KeyValueDatabase_1.buildEnv)({
37
+ default: (0, ConfigCluster_1.defineLazyInitConfig)((_, compilation) => {
38
+ const url = compilation.compilationEnv.read('DOMAIN_HOST');
39
+ return url.href;
40
+ }),
41
+ checker(value) {
42
+ if (this.manual)
43
+ return { value, message: '不应当手动设置该项!' };
44
+ return false;
45
+ },
46
+ }),
35
47
  /** 永久缓存标记 */
36
48
  INFINITE_CACHE: (0, KeyValueDatabase_1.buildEnv)({
37
49
  default: Symbol(),
@@ -84,8 +84,14 @@ function buildCommon() {
84
84
  const headers = response.headers;
85
85
  if (headers.has(INVALID_KEY))
86
86
  return false;
87
- if (rule === INFINITE_CACHE)
88
- return true;
87
+ // 只有本站资源允许永久缓存
88
+ if (rule === INFINITE_CACHE) {
89
+ const url = response.url;
90
+ const baseLength = BASE_URL.length;
91
+ if (url.startsWith(BASE_URL) && (url.length === baseLength || url[baseLength] === '/')) {
92
+ return true;
93
+ }
94
+ }
89
95
  const storage = headers.get(STORAGE_TIMESTAMP);
90
96
  if (!storage)
91
97
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "3.0.0",
3
+ "version": "3.1.0-beta2",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",