Skip to content

fix(font): 修复 Astro Font API 组合字体变量的 CJK 回退#502

Merged
Leaf-0w0 merged 1 commit into
LyraVoid:masterfrom
Dawn6666666:fix/composable-astro-font-fallbacks
Jul 13, 2026
Merged

fix(font): 修复 Astro Font API 组合字体变量的 CJK 回退#502
Leaf-0w0 merged 1 commit into
LyraVoid:masterfrom
Dawn6666666:fix/composable-astro-font-fallbacks

Conversation

@Dawn6666666

Copy link
Copy Markdown
Contributor

问题

Astro Font API 为 --font-body--font-cjk 分别生成完整的 fallback 链。主题再通过下列配置组合它们:

--font-sans: var(--font-body), var(--font-cjk), ui-sans-serif, system-ui, sans-serif;

展开后,--font-body 自带的 sans-serif 会在 --font-cjk 之前生效。当英文字体缺少中文字符时,浏览器会直接回退到系统字体,而不会继续尝试 CJK 字体,导致“英文优先 ZenMaru、中文优先 Loli”的配置失效。

解决方案

保留 Astro Font API 的字体加载、指纹化和预加载能力,但让两个待组合的 CSS 变量仅表示各自的字体:

  • fallbacks 设为 []
  • optimizedFallbacks 设为 false,避免生成度量优化后的本地 fallback;
  • 最终系统字体回退继续只由 --font-sans 统一提供。

这样最终字体顺序变为:

ZenMaruGothic → Loli → ui-sans-serif → system-ui → sans-serif

验证

  • pnpm exec astro check:0 errors、0 warnings。
  • 构建产物中 --font-body--font-cjk 均只包含对应的 Astro 字体名。
  • 使用 ZenMaru 缺失、但 Loli 包含的中文字符进行本地对照:修复前落入系统字体,修复后正确使用 Loli。

Related to #489

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the Astro Font API configuration so the generated --font-body and --font-cjk CSS variables contain only their respective font names (no embedded fallback chain), allowing the project’s composed --font-sans stack to correctly fall back from Latin to CJK fonts before reaching system fonts.

Changes:

  • Set fallbacks: [] for --font-body and --font-cjk so they don’t inject generic/system fallbacks ahead of the next font in the composed stack.
  • Disable optimizedFallbacks for both fonts to avoid generating additional local optimized fallbacks that would interfere with ordering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread astro.config.mjs
Comment on lines +58 to +60
// These variables are composed into --font-sans below. Keep their
// fallback lists empty; otherwise a system fallback after this Latin
// font prevents the following CJK font from ever being considered.
Comment thread astro.config.mjs
Comment on lines +77 to +78
// The final system fallback belongs to --font-sans, not this partial
// CJK font stack.
@Leaf-0w0 Leaf-0w0 merged commit c5aaa63 into LyraVoid:master Jul 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants