feat(theme): add repo card cache (#338)

This commit is contained in:
pengzhanbo 2024-11-18 00:40:58 +08:00 committed by GitHub
parent 3a73312414
commit c4438afaa5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,5 @@
import type { MaybeRef } from 'vue'
import { useLocalStorage } from '@vueuse/core'
import { computed, ref, toValue, watch } from 'vue'
export interface GithubRepoInfo {
@ -20,6 +21,15 @@ export interface GithubRepoInfo {
} | null
}
/**
* github repo api vercel 使
* 6
*/
const storage = useLocalStorage('github-repo', {} as Record<string, {
info: GithubRepoInfo
updatedAt: number
}>)
export function useGithubRepo(repo: MaybeRef<string>) {
const repoRef = computed(() => {
const info = toValue(repo)
@ -34,12 +44,25 @@ export function useGithubRepo(repo: MaybeRef<string>) {
if (__VUEPRESS_SSR__ || !owner || !name)
return
const key = `${owner}/${name}`
const cached = storage.value[`${owner}/${name}`]
if (cached && Date.now() - cached.updatedAt <= 21600000) {
data.value = cached.info
loaded.value = true
return
}
loaded.value = false
const res = await fetch(`https://api.pengzhanbo.cn/github/repo/${owner}/${name}`)
.then(res => res.json()) as GithubRepoInfo
data.value = res
loaded.value = true
storage.value[key] = {
info: res,
updatedAt: Date.now(),
}
}
watch(repoRef, fetchData, { immediate: true })