From 8161a3f75fe68da8775a9b382aa18f44720adcc1 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 7 May 2026 13:27:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=AE=88=E5=8D=AB=E5=AE=89=E8=A3=85=E6=A3=80=E6=9F=A5=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20localStorage=20=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将安装状态缓存到 localStorage,有效期 24 小时 - 避免每次路由切换都发起 HTTP 请求 - 显著提升页面切换速度 --- frontend/src/router/guard/index.ts | 47 +++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/frontend/src/router/guard/index.ts b/frontend/src/router/guard/index.ts index a4123e6..11f3dc2 100644 --- a/frontend/src/router/guard/index.ts +++ b/frontend/src/router/guard/index.ts @@ -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 { - 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 } }