perf: 优化路由守卫安装检查,使用 localStorage 缓存

- 将安装状态缓存到 localStorage,有效期 24 小时
- 避免每次路由切换都发起 HTTP 请求
- 显著提升页面切换速度
This commit is contained in:
2026-05-07 13:27:13 +08:00
parent 8811511ff8
commit 8161a3f75f
+37 -10
View File
@@ -1,25 +1,52 @@
import type { Router } from 'vue-router'
let installChecked = false
let isInstalled = false
const INSTALL_CACHE_KEY = 'app_installed'
const INSTALL_CACHE_EXPIRY = 24 * 60 * 60 * 1000
function getCachedInstallStatus(): boolean | null {
try {
const cached = localStorage.getItem(INSTALL_CACHE_KEY)
if (cached) {
const { installed, timestamp } = JSON.parse(cached)
if (Date.now() - timestamp < INSTALL_CACHE_EXPIRY) {
return installed
}
}
}
catch {
return null
}
return null
}
function setCachedInstallStatus(installed: boolean) {
try {
localStorage.setItem(INSTALL_CACHE_KEY, JSON.stringify({
installed,
timestamp: Date.now(),
}))
}
catch {
}
}
export function resetInstallCheck() {
installChecked = false
isInstalled = false
localStorage.removeItem(INSTALL_CACHE_KEY)
}
async function checkInstallStatus(): Promise<boolean> {
if (installChecked) return isInstalled
const cached = getCachedInstallStatus()
if (cached !== null) return cached
try {
const res = await fetch('/api/install/status')
const data = await res.json()
isInstalled = data?.data?.installed ?? false
installChecked = true
return isInstalled
const installed = data?.data?.installed ?? false
setCachedInstallStatus(installed)
return installed
}
catch {
installChecked = true
isInstalled = false
setCachedInstallStatus(false)
return false
}
}