Skip to content

Commit a5814d5

Browse files
committed
chore: 添加脚本,以跨package.jsonversion.json和服务工作者缓存名称自动更新版本。
1 parent eed53b7 commit a5814d5

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

next.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { NextConfig } from 'next';
2+
import { updateVersion } from './scripts/update-version';
3+
4+
// 自动更新版本号
5+
updateVersion();
26

37
const nextConfig: NextConfig = {
48
// 启用静态导出

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frontend-navigation-site",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
@@ -49,4 +49,4 @@
4949
"tailwindcss": "^4.0.0",
5050
"typescript": "^5.6.0"
5151
}
52-
}
52+
}

public/sw.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Service Worker for PWA
2-
const CACHE_NAME = 'weiz-nav-v4';
3-
const RUNTIME_CACHE = 'weiz-nav-runtime-v4';
4-
const IMAGE_CACHE = 'weiz-nav-images-v4';
2+
const CACHE_NAME = 'weiz-nav-v0_1_1';
3+
const RUNTIME_CACHE = 'weiz-nav-runtime-v0_1_1';
4+
const IMAGE_CACHE = 'weiz-nav-images-v0_1_1';
55

66
// 需要预缓存的静态资源
77
const PRECACHE_URLS = [

public/version.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"version": "1.0.4",
3-
"buildTime": "2025-11-19T15:26:00.000Z",
4-
"cacheVersion": "v4",
2+
"version": "0.1.1",
3+
"buildTime": "2025-12-02T08:58:52.853Z",
4+
"cacheVersion": "v0_1_1",
55
"changelog": [
66
"修复 CSP 策略,支持所有 HTTPS 资源",
77
"优化图标缓存策略",

scripts/update-version.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* 更新版本号
6+
* 读取 package.json 的版本号,同步到 public/version.json 和 public/sw.js
7+
*/
8+
export function updateVersion() {
9+
// 路径配置
10+
const PATHS = {
11+
package: path.join(process.cwd(), 'package.json'),
12+
version: path.join(process.cwd(), 'public/version.json'),
13+
sw: path.join(process.cwd(), 'public/sw.js'),
14+
};
15+
16+
try {
17+
// 读取 package.json
18+
const pkg = JSON.parse(fs.readFileSync(PATHS.package, 'utf8'));
19+
const version = pkg.version;
20+
const cacheVersion = `v${version.replace(/\./g, '_')}`; // e.g., 1.0.5 -> v1_0_5
21+
22+
// 1. 更新 public/version.json
23+
let versionData: any = {};
24+
if (fs.existsSync(PATHS.version)) {
25+
versionData = JSON.parse(fs.readFileSync(PATHS.version, 'utf8'));
26+
}
27+
28+
// 检查是否需要更新
29+
if (versionData.version === version) {
30+
// 版本号相同,不更新
31+
return;
32+
}
33+
34+
console.log(`Updating version to ${version} (Cache: ${cacheVersion})...`);
35+
36+
versionData.version = version;
37+
versionData.buildTime = new Date().toISOString();
38+
versionData.cacheVersion = cacheVersion;
39+
40+
fs.writeFileSync(PATHS.version, JSON.stringify(versionData, null, 2) + '\n');
41+
console.log('✓ Updated public/version.json');
42+
43+
// 2. 更新 public/sw.js
44+
let swContent = fs.readFileSync(PATHS.sw, 'utf8');
45+
46+
// 更新 CACHE_NAME
47+
swContent = swContent.replace(
48+
/const CACHE_NAME = ['"]weiz-nav-v[^'"]+['"];/,
49+
`const CACHE_NAME = 'weiz-nav-${cacheVersion}';`
50+
);
51+
52+
// 更新 RUNTIME_CACHE
53+
swContent = swContent.replace(
54+
/const RUNTIME_CACHE = ['"]weiz-nav-runtime-v[^'"]+['"];/,
55+
`const RUNTIME_CACHE = 'weiz-nav-runtime-${cacheVersion}';`
56+
);
57+
58+
// 更新 IMAGE_CACHE
59+
swContent = swContent.replace(
60+
/const IMAGE_CACHE = ['"]weiz-nav-images-v[^'"]+['"];/,
61+
`const IMAGE_CACHE = 'weiz-nav-images-${cacheVersion}';`
62+
);
63+
64+
fs.writeFileSync(PATHS.sw, swContent);
65+
console.log('✓ Updated public/sw.js');
66+
67+
console.log('Version update complete!');
68+
} catch (error) {
69+
console.error('✗ Failed to update version:', error);
70+
// 不抛出错误,以免影响构建流程
71+
}
72+
}

0 commit comments

Comments
 (0)