VersionChecker 是一个自动版本检测组件,用于检测应用是否有新版本可用,并在检测到新版本时提示用户更新。
import VersionChecker from '@/components/VersionChecker';
const App = () => {
const currentBuildTime = process.env.REACT_APP_BUILD_TIME;
return (
<div>
{/* 你的应用内容 */}
<VersionChecker currentBuildTime={currentBuildTime} />
</div>
);
};
<VersionChecker
currentBuildTime={process.env.REACT_APP_BUILD_TIME}
checkInterval={30 * 1000} // 30秒检查一次
notificationCooldown={10 * 60 * 1000} // 10分钟内不重复提示
/>
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
currentBuildTime |
string \| number |
✅ | - | 当前应用的构建时间戳 |
checkInterval |
number |
❌ | 60000 |
检查间隔(毫秒) |
notificationCooldown |
number |
❌ | 300000 |
通知冷却时间(毫秒) |
fetch('/version.json') 获取远程版本信息buildTimestamp 与本地 REACT_APP_BUILD_TIME组件期望的 version.json 文件格式:
{
"productId": "go-pmp",
"version": "1.0.0 build:ABC123(UTC+8 北京时间: 2024-01-15 14:30:25)",
"buildTime": "2024-01-15 14:30:25",
"buildTimestamp": 1705302625,
"environment": "prod"
}
更新通知使用 Ant Design 的 notification 组件,包含:
确保在构建时设置了 REACT_APP_BUILD_TIME 环境变量:
# 构建时设置
REACT_APP_BUILD_TIME=1705302625 npm run build
/version.json 文件在浏览器控制台中可以看到详细的日志信息:
版本检查已启动,检查间隔: 60秒
获取到版本信息: {productId: "go-pmp", version: "1.0.0", ...}
版本检查 - 远程构建时间: 1705302625
版本检查 - 本地构建时间: 1705300000
检测到新版本可用
REACT_APP_BUILD_TIME 环境变量是否正确设置/version.json 文件是否可以正常访问notificationCooldown 参数buildTimestamp 是否正确version.json 文件已更新buildTimestamp 字段是否存在且为数字类型