Font Optimization (next/font)
<link> to Google Fonts adds an extra round trip to a third-party server, and until that request resolves, text either stays invisible or renders in a fallback font and then visibly "jumps" once the real font arrives. The next/font module solves both problems by downloading and self-hosting fonts as part of your build.next/font serves font files from your own domain instead of Google's servers, there is no request ever sent to an external origin at runtime — no extra DNS lookup, no third-party connection, and no layout tracking script involved. This is also a privacy win, since the visitor's browser never talks to Google Fonts directly.next/font/google
next/font/google. Next.js downloads the font files at build time and serves them alongside your other static assets.app/layout.tsx
import { Inter } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={inter.variable}>
<body className={inter.className}>{children}</body>
</html>
)
}variable option exposes the font as a CSS custom property (--font-inter here), which you can reference from global CSS, CSS Modules, or a CSS-in-JS theme — handy when you want the font available outside the component tree that imported it.next/font/local
next/font/local gives the same self-hosting and optimization benefits for files sitting in your project.app/layout.tsx
import localFont from 'next/font/local'
const myFont = localFont({
src: './fonts/MyBrandFont.woff2',
variable: '--font-brand',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html className={myFont.variable}>
<body className={myFont.className}>{children}</body>
</html>
)
}Automatic layout-shift prevention
next/font automatically computes fallback font metrics (like size-adjust) so that the space reserved for text is nearly identical whether the fallback system font or the final custom font is showing. This drastically reduces — often eliminates — the visible reflow that happens when a web font swaps in, which directly improves Cumulative Layout Shift (CLS), a Core Web Vital.Approach | Extra network request? | Layout shift handling |
|---|---|---|
| Yes, to fonts.googleapis.com | Manual, if handled at all |
| No — self-hosted at build time | Automatic fallback metric matching |
| No — served from your own assets | Automatic fallback metric matching |
variable option and set the class on <html> (rather than only <body>) when a component library or CSS-in-JS theme needs to read the font variable from anywhere in the tree, not just inside the layout that loaded it.next/fontself-hosts font files — including Google Fonts — at build time, so no request ever goes to an external font server at runtime.next/font/googlefetches from the Google Fonts catalog;next/font/localoptimizes font files you already own.It automatically applies fallback font metric adjustments, minimizing layout shift while the real font loads.
The
variableoption exposes the font as a CSS custom property usable from any styling approach.