const LOCAL_VERSION_KEY = 'version' /** * 获取最新的构建时间,于当前 localStorage 中的时间对比 */ export async function checkVersion() { try { // 每次构建时根目录生成 verison.txt,内容为构建时的时间戳 const fetchVersion = await fetch('/version.txt', { cache: 'no-store' }) let newVersion = await fetchVersion.text() newVersion = newVersion.trim() const localVersion = localStorage.getItem(LOCAL_VERSION_KEY) if (localVersion != null && localVersion < newVersion) { const userConfirmed = window.confirm('检测到新版本,请刷新') if (userConfirmed) { localStorage.setItem(LOCAL_VERSION_KEY, newVersion) window.location.href = `/?v=${Date.now()}` } } else { localStorage.setItem(LOCAL_VERSION_KEY, newVersion) } } catch (error) { console.error('检查版本失败', error) } } /** * 区分环境设置检测周期 */ export function getInterval() { if (import.meta.env.DEV) { return 60 * 1000 } // 生产或演示环境 5 分钟 return 5 * 60 * 1000 }