update at 2026-02-11 18:37:30

This commit is contained in:
douboer
2026-02-11 18:37:30 +08:00
parent 2a737f2857
commit 74eebc19db
2 changed files with 35 additions and 12 deletions

View File

@@ -82,8 +82,8 @@ server {
location = /fonts.json { location = /fonts.json {
expires 1h; expires 1h;
add_header Cache-Control "public, must-revalidate" always; add_header Cache-Control "public, must-revalidate" always;
# Web 端固定请求 /fonts.json优先使用 dist 内清单,兼容仅部署 dist 的场景 # Web 端固定请求 /fonts.json依次回退到 dist/public/root 三处清单
try_files /frontend/dist/fonts.json /fonts.json =404; try_files /frontend/dist/fonts.json /frontend/public/fonts.json /fonts.json =404;
} }
location = /miniprogram/assets/fonts.json { location = /miniprogram/assets/fonts.json {

View File

@@ -13,20 +13,43 @@ interface FontListItem {
export function useFontLoader() { export function useFontLoader() {
const fontStore = useFontStore() const fontStore = useFontStore()
async function fetchFontListWithFallback(): Promise<FontListItem[]> {
const candidates = ['/fonts.json', '/frontend/dist/fonts.json']
const errors: string[] = []
for (const url of candidates) {
const requestUrl = `${url}?_ts=${Date.now()}`
try {
console.log(`Fetching ${requestUrl}...`)
const response = await fetch(requestUrl, { cache: 'no-store' })
console.log(`${url} response:`, response.status, response.statusText)
if (!response.ok) {
errors.push(`${url}: HTTP ${response.status}`)
continue
}
const data = await response.json()
if (!Array.isArray(data)) {
errors.push(`${url}: JSON 不是数组`)
continue
}
return data as FontListItem[]
} catch (error) {
errors.push(`${url}: ${error instanceof Error ? error.message : String(error)}`)
}
}
throw new Error(errors.join(' | '))
}
async function loadFontList() { async function loadFontList() {
console.log('Starting to load font list...') console.log('Starting to load font list...')
fontStore.isLoadingFonts = true fontStore.isLoadingFonts = true
try { try {
console.log('Fetching /fonts.json...') const fontList = await fetchFontListWithFallback()
const response = await fetch('/fonts.json')
console.log('Response status:', response.status, response.statusText)
if (!response.ok) {
throw new Error(`Failed to load fonts.json: ${response.statusText}`)
}
const fontList: FontListItem[] = await response.json()
console.log('Loaded font list:', fontList.length, 'fonts') console.log('Loaded font list:', fontList.length, 'fonts')
// 转换为 FontInfo // 转换为 FontInfo
@@ -50,7 +73,7 @@ export function useFontLoader() {
console.log(`Successfully loaded ${fontList.length} fonts`) console.log(`Successfully loaded ${fontList.length} fonts`)
} catch (error) { } catch (error) {
console.error('Failed to load font list:', error) console.error('Failed to load font list:', error)
alert('加载字体列表失败,请刷新页面重试') alert(`加载字体列表失败${error instanceof Error ? error.message : '未知错误'}`)
} finally { } finally {
fontStore.isLoadingFonts = false fontStore.isLoadingFonts = false
console.log('Font loading finished') console.log('Font loading finished')