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 +38 -38
- package/dist/swpp/BasicActions.d.ts +0 -3
- package/dist/swpp/BasicActions.js +2 -71
- package/dist/swpp/FilePath.d.ts +4 -0
- package/dist/swpp/FilePath.js +6 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,55 +4,55 @@
|
|
|
4
4
|
|
|
5
5
|
## 什么是 swpp
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
swpp(Service Worker Plus Plus)是一个现代化的 Service Worker 生成工具,专注于为静态网站提供精准、高效的缓存控制和离线访问能力。
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
与传统的 Service Worker 工具相比,swpp 提供了更细粒度的控制能力和更灵活的配置选项,让开发者能够精确管理网站资源的缓存策略。
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|:----------:|:------:|:------:|:------------:|
|
|
13
|
-
| 本地缓存 | ✔️ | ✔️ | ✔️ |
|
|
14
|
-
| 缓存增量更新 | ✔️ | ✔️ | ❌ |
|
|
15
|
-
| 缓存过期时间 | ✔️ | ❌ | ✔️ |
|
|
16
|
-
| 缓存大小限制 | ❌ | ❌ | ✔️ |
|
|
17
|
-
| 预缓存 | ❌ | ❌ | ✔️ |
|
|
18
|
-
| Request 篡改 | ✔️ | ✔️ | ❌ |
|
|
19
|
-
| URL 竞速 | ✔️ | ✔️ | ❌ |
|
|
20
|
-
| 备用 URL | ✔️ | ✔️ | ❌ |
|
|
21
|
-
| 204 阻塞响应 | ✔️ | ✔️ | ❌ |
|
|
22
|
-
| 逃生门 | ✔️ | ✔️ | ❌ |
|
|
23
|
-
| 请求合并 | ❌ | ✔️ | ❌ |
|
|
24
|
-
| 高度自由 | ✔️ | ✔️ | ❌ |
|
|
25
|
-
| 更新 | 活跃 | 停止维护 | 超过两年没有更新 |
|
|
11
|
+
## 核心特性
|
|
26
12
|
|
|
27
|
-
|
|
13
|
+
### 精准缓存控制
|
|
14
|
+
- **智能缓存规则匹配**:基于 URL 模式的精细化缓存策略配置
|
|
15
|
+
- **缓存过期时间管理**:可为不同类型的资源设置独立的过期时间
|
|
16
|
+
- **增量更新机制**:只更新发生变化的资源,避免全量刷新
|
|
28
17
|
|
|
29
|
-
|
|
18
|
+
### 性能优化
|
|
19
|
+
- **URL 竞速加载**:同时请求多个 CDN 源,使用最快响应的资源
|
|
20
|
+
- **备用 URL 机制**:主资源加载失败时自动切换到备用地址
|
|
30
21
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
22
|
+
### 高度可定制
|
|
23
|
+
- **模块化配置**:将 Service Worker 功能拆分为独立的配置模块
|
|
24
|
+
- **TypeScript 支持**:完整的类型定义,提供更好的开发体验
|
|
25
|
+
- **全面开放**:允许用户修改生成的 Service Worker 文件中任意一处代码
|
|
34
26
|
|
|
35
|
-
-
|
|
36
|
-
现在您可以使用 `ts` 语言编写配置文件,同时还可以选择使用 `ESM`。在 v2 中您必须使用 `CommonJS`。<br/>
|
|
37
|
-
同时使用内置的一系列 `define` 函数可以让您对配置的颗粒度进行更加完美的掌控。
|
|
27
|
+
## swpp 与 hexo-offline 对比
|
|
38
28
|
|
|
39
|
-
-
|
|
40
|
-
|
|
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
|
-
|
|
44
|
+
### 选择建议
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
- `alpha` - 测试版本
|
|
48
|
-
- `a` - 一级子版本号,变动时表明 API 或配置文件等内容发生了不向前兼容的变动
|
|
49
|
-
- `b` - 二级子版本号,变动时表明添加了新的功能
|
|
50
|
-
- `c` - 三级子版本号,变动时表明进行了漏洞修复
|
|
46
|
+
如果你:
|
|
51
47
|
|
|
52
|
-
|
|
48
|
+
- 希望对动态资源(如 API 响应、用户内容)的缓存进行精细策略控制;
|
|
49
|
+
- 重视代码的透明度和可审计性,希望完全理解底层逻辑;
|
|
50
|
+
- 享受深入技术细节、亲手构建网络层控制逻辑的过程;
|
|
51
|
+
- 在个人项目或技术实验中探索 Service Worker 的潜力
|
|
53
52
|
|
|
54
|
-
|
|
53
|
+
那么 swpp 是一个非常值得一试的选择。
|
|
55
54
|
|
|
56
|
-
|
|
55
|
+
## 社区支持
|
|
57
56
|
|
|
58
|
-
|
|
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
|
|
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
|
|
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()) {
|
package/dist/swpp/FilePath.d.ts
CHANGED
package/dist/swpp/FilePath.js
CHANGED