Performance: Font optimization
HIGH IMPACT
Font optimization affects page load speed and visual stability by controlling how fonts are loaded and rendered.
--- // Preload key font in head const preloadFont = () => { return `<link rel="preload" href="/fonts/myfont.woff2" as="font" type="font/woff2" crossorigin="anonymous" />`; }; --- <head> {Astro.raw(preloadFont())} <style> @font-face { font-family: 'MyFont'; src: url('/fonts/myfont.woff2') format('woff2'); font-display: swap; font-weight: 400; font-style: normal; } </style> </head>
import './styles.css'; /* styles.css includes @font-face with multiple font weights and styles, no preload or font-display set */
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all fonts without preload or font-display | Minimal | Multiple reflows due to late font swap | High paint cost from layout shifts | [X] Bad |
| Preload key fonts and use font-display: swap | Minimal | Single reflow or none | Low paint cost with stable layout | [OK] Good |