fix: opt home title load

This commit is contained in:
engigu
2026-01-03 13:15:58 +08:00
parent ccf79ad364
commit 1eaca4f4a9
2 changed files with 76 additions and 5 deletions
+38 -2
View File
@@ -1,7 +1,32 @@
import { ref, computed } from 'vue'
import { api, type SiteSettings } from '@/api'
const siteSettings = ref<SiteSettings>({
const CACHE_KEY = 'site_settings_cache'
// 从 localStorage 加载缓存
function loadFromCache(): SiteSettings | null {
try {
const cached = localStorage.getItem(CACHE_KEY)
if (cached) {
return JSON.parse(cached)
}
} catch {
// 忽略解析错误
}
return null
}
// 保存到 localStorage
function saveToCache(settings: SiteSettings) {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify(settings))
} catch {
// 忽略存储错误
}
}
const cachedSettings = loadFromCache()
const siteSettings = ref<SiteSettings>(cachedSettings || {
title: '白虎面板',
subtitle: '轻量级定时任务管理系统',
icon: '',
@@ -9,6 +34,16 @@ const siteSettings = ref<SiteSettings>({
cookie_days: '7'
})
// 立即应用缓存的设置
if (cachedSettings) {
if (cachedSettings.title) {
document.title = cachedSettings.title
}
if (cachedSettings.icon) {
updateFavicon(cachedSettings.icon)
}
}
let loaded = false
function updateFavicon(svgContent: string) {
@@ -35,11 +70,12 @@ export function useSiteSettings() {
try {
const res = await api.settings.getSite()
siteSettings.value = res
saveToCache(res) // 保存到缓存
document.title = res.title || '白虎面板'
if (res.icon) updateFavicon(res.icon)
loaded = true
} catch {
// 使用默认值
// 使用默认值或缓存值
}
}