perf: preload and cache countdowns
ci / docker (push) Successful in 3m49s

This commit is contained in:
2026-09-07 17:50:26 +08:00
parent 635930db50
commit a02b6f660c
6 changed files with 64 additions and 5 deletions
+20 -2
View File
@@ -2,7 +2,7 @@
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { Archive, ArchiveRestore, CalendarHeart, ChevronDown, Pencil, Pin, Trash2, X } from 'lucide-vue-next'
import { csrfHeader } from './lib/csrf'
import { calendarModeLabel, countdownDayText, countdownKindLabel, dateKey, formatApiErrorDetail } from './lib/mvp-utils'
import { calendarModeLabel, countdownDayText, countdownKindLabel, dateKey, formatApiErrorDetail, readCountdownCache, writeCountdownCache } from './lib/mvp-utils'
type Countdown = {
id: string; title: string; event_date: string; display_date: string; kind: 'countdown'|'anniversary'|'birthday'
@@ -99,7 +99,25 @@ async function request(path:string, options:RequestInit={}) {
return response.status === 204 ? null : response.json()
}
async function safe(work:()=>Promise<void>) { busy.value=true; error.value=''; try { await work() } catch(reason) { error.value=reason instanceof Error ? reason.message : '请求失败' } finally { busy.value=false } }
async function load() { await safe(async()=>{ const [a,b]=await Promise.all([request('/countdowns'),request('/countdowns?archived=true')]); items.value=a; archived.value=b }) }
async function load() {
const cached = readCountdownCache<Countdown>()
if (cached) { items.value=cached.items; archived.value=cached.archived }
if (!cached) busy.value=true
error.value=''
try {
const active = await request('/countdowns') as Countdown[]
items.value=active
writeCountdownCache(items.value, archived.value)
busy.value=false
const archivedItems = await request('/countdowns?archived=true') as Countdown[]
archived.value=archivedItems
writeCountdownCache(items.value, archived.value)
} catch(reason) {
error.value=reason instanceof Error ? reason.message : '请求失败'
} finally {
busy.value=false
}
}
function edit(item:Countdown) {
detailItem.value=null
editingId.value=item.id