From 74eebc19dbe5edde83a6cde316ae1714585e2ddb Mon Sep 17 00:00:00 2001 From: douboer Date: Wed, 11 Feb 2026 18:37:30 +0800 Subject: [PATCH] update at 2026-02-11 18:37:30 --- font2svg.mac.conf | 4 +-- frontend/src/composables/useFontLoader.ts | 43 +++++++++++++++++------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/font2svg.mac.conf b/font2svg.mac.conf index 367482f..1689f5d 100644 --- a/font2svg.mac.conf +++ b/font2svg.mac.conf @@ -82,8 +82,8 @@ server { location = /fonts.json { expires 1h; add_header Cache-Control "public, must-revalidate" always; - # Web 端固定请求 /fonts.json,优先使用 dist 内清单,兼容仅部署 dist 的场景 - try_files /frontend/dist/fonts.json /fonts.json =404; + # Web 端固定请求 /fonts.json,依次回退到 dist/public/root 三处清单 + try_files /frontend/dist/fonts.json /frontend/public/fonts.json /fonts.json =404; } location = /miniprogram/assets/fonts.json { diff --git a/frontend/src/composables/useFontLoader.ts b/frontend/src/composables/useFontLoader.ts index 1667096..50c96f4 100644 --- a/frontend/src/composables/useFontLoader.ts +++ b/frontend/src/composables/useFontLoader.ts @@ -13,20 +13,43 @@ interface FontListItem { export function useFontLoader() { const fontStore = useFontStore() + async function fetchFontListWithFallback(): Promise { + 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() { console.log('Starting to load font list...') fontStore.isLoadingFonts = true try { - console.log('Fetching /fonts.json...') - 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() + const fontList = await fetchFontListWithFallback() console.log('Loaded font list:', fontList.length, 'fonts') // 转换为 FontInfo @@ -50,7 +73,7 @@ export function useFontLoader() { console.log(`Successfully loaded ${fontList.length} fonts`) } catch (error) { console.error('Failed to load font list:', error) - alert('加载字体列表失败,请刷新页面重试') + alert(`加载字体列表失败:${error instanceof Error ? error.message : '未知错误'}`) } finally { fontStore.isLoadingFonts = false console.log('Font loading finished')