swpp-backends 3.1.0-beta1 → 3.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/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(官方技术支持群)
@@ -7,7 +7,6 @@ export declare class BasicActions {
7
7
  private readonly isBuildServiceWorker;
8
8
  private readonly domJsPath;
9
9
  private readonly diffJsonPath;
10
- private readonly disableTrack;
11
10
  /**
12
11
  * 构建一个基础的 swpp 行为封装器
13
12
  */
@@ -67,8 +66,6 @@ export interface BasicActionOptions {
67
66
  domJsPath?: string;
68
67
  /** diff.json 文件路径(相对于网站根目录,留空表示不生成) */
69
68
  diffJsonPath?: string;
70
- /** 是否进行引用的静态分析,留空表示不进行(禁用静态分析后不能使用无限期缓存) */
71
- trackLink?: boolean;
72
69
  }
73
70
  interface BuildFileInfo {
74
71
  key: BasicActionKey;
@@ -12,7 +12,7 @@ class BasicActions {
12
12
  * 构建一个基础的 swpp 行为封装器
13
13
  */
14
14
  static async build(optional) {
15
- const actions = new BasicActions(optional.context, optional.isServiceWorker, optional.domJsPath, optional.diffJsonPath, optional.trackLink !== true);
15
+ const actions = new BasicActions(optional.context, optional.isServiceWorker, optional.domJsPath, optional.diffJsonPath);
16
16
  await actions.configLoader.loadFromCode({
17
17
  compilationEnv: {
18
18
  PUBLIC_PATH: (0, ConfigCluster_1.defineLazyInitConfig)((_, compilation) => {
@@ -22,12 +22,11 @@ class BasicActions {
22
22
  });
23
23
  return actions;
24
24
  }
25
- constructor(context, isBuildServiceWorker, domJsPath, diffJsonPath, disableTrack) {
25
+ constructor(context, isBuildServiceWorker, domJsPath, diffJsonPath) {
26
26
  this.context = context;
27
27
  this.isBuildServiceWorker = isBuildServiceWorker;
28
28
  this.domJsPath = domJsPath;
29
29
  this.diffJsonPath = diffJsonPath;
30
- this.disableTrack = disableTrack;
31
30
  this.configLoader = new ConfigLoader_1.ConfigLoader(this.context);
32
31
  /**
33
32
  * 各个文件的生成路径,为空则表示不生成(或配置未初始化)
@@ -40,12 +39,6 @@ class BasicActions {
40
39
  diffJson: null
41
40
  };
42
41
  this.buildCaches = {};
43
- if (disableTrack) {
44
- untils_1.utils.printInfo('Track', '链接追踪(静态分析)已被关闭,缓存主动更新已自动禁用,请勿使用永久缓存');
45
- }
46
- else {
47
- untils_1.utils.printInfo('Track', '链接追踪(静态分析)已开启,如果发现行为异常请及时反馈');
48
- }
49
42
  }
50
43
  /**
51
44
  * 加载一个配置文件或配置
@@ -82,57 +75,6 @@ class BasicActions {
82
75
  this.runtimeData = runtime;
83
76
  this.compilationData = compilation;
84
77
  this.configLoader = undefined;
85
- if (this.disableTrack) {
86
- const matcher = runtime.crossDep.read('matchCacheRule');
87
- const checker = (text) => {
88
- if (!text.includes('INFINITE_CACHE'))
89
- return true;
90
- const list = text.replaceAll('\r', '').split('\n');
91
- let inStr = '';
92
- let inComment = false;
93
- o: for (let string of list) {
94
- for (let i = 0; i < string.length; i++) {
95
- if (inStr) {
96
- if (string[i] == inStr)
97
- inStr = '';
98
- }
99
- else if (inComment) {
100
- if (string[i] == '*' && string[i + 1] === '/') {
101
- inComment = false;
102
- ++i;
103
- }
104
- }
105
- else {
106
- switch (string[i]) {
107
- case '"':
108
- case '`':
109
- case `"`:
110
- inStr = string[i];
111
- continue;
112
- case '/':
113
- if (string[i + 1] === '*') {
114
- inComment = true;
115
- }
116
- else if (string[i + 1] === '/') {
117
- continue o;
118
- }
119
- break;
120
- default:
121
- if (string.startsWith('INFINITE_CACHE', i)) {
122
- return false;
123
- }
124
- break;
125
- }
126
- }
127
- }
128
- }
129
- return true;
130
- };
131
- if (!checker(matcher.runOnBrowser.toString())) {
132
- throw new untils_1.RuntimeException(untils_1.exceptionNames.unsupportedOperate, '禁用引用链分析时禁止在 matchCacheRule 返回 INFINITE_CACHE。' +
133
- '如果您没有返回该值,可能是由于存在与该字段同名的变量或其它内容,从而导致 SWPP 误判,请您修改与 INFINITE_CACHE 重复的名称。');
134
- }
135
- }
136
78
  const compilationEnv = compilation.compilationEnv;
137
79
  const publicRoot = compilationEnv.read('PUBLIC_PATH');
138
80
  const jsonInfo = compilationEnv.read('SWPP_JSON_FILE');
@@ -220,17 +162,6 @@ class BasicActions {
220
162
  * @param excludeFilter 需排除的文件
221
163
  */
222
164
  async saveFiles(excludeFilter = []) {
223
- if (this.disableTrack) {
224
- if (!excludeFilter.includes('tracker')) {
225
- excludeFilter.push('tracker');
226
- }
227
- if (!excludeFilter.includes('version')) {
228
- excludeFilter.push('version');
229
- }
230
- if (!excludeFilter.includes('diffJson')) {
231
- excludeFilter.push('diffJson');
232
- }
233
- }
234
165
  const fileList = await this.buildFiles(excludeFilter);
235
166
  for (let item of fileList) {
236
167
  if (await item.path.exists()) {
@@ -39,6 +39,10 @@ export declare class FilePath {
39
39
  * @param consumer
40
40
  */
41
41
  walkAllFile(consumer: (filePath: FilePath) => Promise<void> | void): Promise<void>;
42
+ /**
43
+ * 获取文件名
44
+ */
45
+ fileName(): string;
42
46
  /**
43
47
  * 获取上一级目录
44
48
  */
@@ -91,6 +91,12 @@ class FilePath {
91
91
  }
92
92
  } while (queue.length);
93
93
  }
94
+ /**
95
+ * 获取文件名
96
+ */
97
+ fileName() {
98
+ return path_1.default.posix.basename(this.absPath);
99
+ }
94
100
  /**
95
101
  * 获取上一级目录
96
102
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "3.1.0-beta1",
3
+ "version": "3.1.0",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",