Performance: Font optimization and self-hosting
HIGH IMPACT
This affects page load speed by reducing font file download time and rendering delays caused by font loading.
import localFont from 'next/font/local'; const roboto = localFont({ src: './fonts/roboto-regular.woff2', display: 'swap' }); export default function App({ Component, pageProps }) { return <main className={roboto.className}> <Component {...pageProps} /> </main>; }
import '../styles/globals.css'; // CSS imports Google Fonts via @import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap'); export default function App({ Component, pageProps }) { return <Component {...pageProps} />; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| External Google Fonts CSS | No extra DOM nodes | Triggers reflow on font swap | High paint cost on font load | [X] Bad |
| Self-hosted fonts with font-display: swap | No extra DOM nodes | Single reflow on initial load | Low paint cost, smooth swap | [OK] Good |