字体加载优化
font-display 取值说明
| 取值 | 阻塞期 | 交换期 | 适用场景 |
|---|---|---|---|
auto | 浏览器决定 | — | 默认行为 |
block | 3s | 无限 | 关键字体(图标字体) |
swap | 0ms | 无限 | 正文字体(SEO友好) |
fallback | 100ms | 3s | 性能与样式的平衡 |
optional | 100ms | 0ms | 装饰性字体 |
最佳字体加载策略
<!-- Step 1: Preload critical fonts -->
<link rel="preload"
href="/fonts/inter-var.woff2"
as="font"
type="font/woff2"
crossorigin>
<!-- Step 2: CSS with font-display -->
@font-face {
font-family: 'Inter';
src: url('/fonts/inter-var.woff2') format('woff2');
font-display: swap;
font-weight: 100 900; /* variable font range */
unicode-range: U+0000-00FF; /* subset: Latin */
}
<!-- Step 3: System font fallback stack -->
body {
font-family: 'Inter', system-ui, -apple-system,
BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
字体子集化(减小体积)
# Install pyftsubset (fonttools) pip install fonttools brotli # Subset to Latin characters only pyftsubset font.ttf \ --output-file=font-subset.woff2 \ --flavor=woff2 \ --unicodes="U+0000-00FF,U+0131,U+0152-0153" # Typical size reduction: 400KB → 30KB for Latin subset
中文Web字体策略
- 中文字体5–30MB,永远不要加载完整字体文件
- 使用Google Fonts的&text=参数加载特定字符
- 考虑Fontsource自托管子集
- 正文字体:依赖系统CJK字体(苹方、微软雅黑)