返回提示词目录第 1174 页

优化网页字体加载性能

盘点字体资源并调整子集、预加载和回退配置,输出代码改动与性能前后对比。

完整提示词

<context>
Fonts hurt performance in three ways: bytes (many weights and styles, full Unicode coverage, legacy formats), discovery (a font is only requested after the CSS that references it is parsed and a matching element is found, so it arrives late), and swap (invisible text while waiting, then a layout shift when the web font replaces a fallback with different metrics). What experts do: ship WOFF2 only, since every current browser supports it; replace several static weights with one variable font when the design uses three or more; subset to the scripts the site actually uses; preload only the one or two files used above the fold, with `crossorigin` (required even on the same origin, otherwise the preload is wasted); choose `font-display: swap` for body text or `optional` where a late swap is worse than the fallback; and tune the fallback with `size-adjust`, `ascent-override`, `descent-override` and `line-gap-override` so the swap barely moves anything. Since browsers partition their caches per site, a shared font CDN no longer saves a download, and self-hosting removes a connection. Icon fonts are a common hidden cost; SVG icons are cheaper.
</context>

<task>
Optimise font loading for: {{fonts}}

1. Inventory every font file actually requested on the key pages: family, weight, style, format, size, where it is declared and which elements use it. Find declared faces that no page uses.
2. Remove unused faces and legacy formats. If three or more weights of one family are used, check whether a variable version exists and compare total bytes.
3. Subset to the character ranges the content needs (with `unicode-range` for multi-script sites so other ranges load only when used). Keep a full set if user-generated content can contain any script, and say so.
4. Self-host if fonts come from a third-party CSS link, unless licence or tooling prevents it; use the framework's font helper if the project has one, since it handles self-hosting, preloading and fallbacks.
5. Preload only the files used in above-the-fold text, with `as="font"`, `type="font/woff2"` and `crossorigin`.
6. Set `font-display` per face and add a fallback `@font-face` with metric overrides computed for the chosen system fallback.
7. Re-measure font bytes, the number of font requests before first paint and layout shift caused by fonts. Run the build and tests and check pages visually.
</task>

<constraints>
- Check the font licence before subsetting or self-hosting a commercial font; if unknown, say it must be checked and do not change hosting.
- Do not drop glyphs the content uses (accents, currency symbols, the languages the site supports).
- Do not preload more than the fonts needed for the first screen; extra preloads compete with the LCP resource.
- Give metric override values as computed from the font files or a tool, never as guesses.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Inventory
Table: family | weight/style | format | KB | used on first screen | keep/remove.
## Changes
One line per file: `path`, what changed.
## Fallback metrics
The fallback `@font-face` rules and how the values were obtained.
## Results
Before and after: font bytes, requests and font-related layout shift.
## Licence check
What the licence allows, or what must be confirmed.
</output_format>

可替换内容

  • {{fonts}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、前端性能、字体加载、布局偏移、资源预加载

前往 AI 图片创作