next/fontNext.js font optimization uses the next/font module to download Google Fonts at build time and serve them from your own domain. Because the browser never contacts external Google font servers, pages render faster and protect user privacy.
Standard stylesheet font links block rendering or swap text sizes abruptly, causing Cumulative Layout Shift (CLS). Next.js measures fallback system font metrics automatically so text stays steady while custom fonts load.
app/layout.tsxImport any Google Font function from next/font/google, configure subsets and CSS variables, and attach the class name to <html> or <body>.
// app/layout.tsx
import { Inter, JetBrains_Mono } from 'next/font/google';
import './globals.css';
const inter = Inter({
subsets: ['latin'],
display: 'swap',
variable: '--font-inter',
});
const jetbrainsMono = JetBrains_Mono({
subsets: ['latin'],
display: 'swap',
variable: '--font-mono',
});
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en" className={`${inter.variable} ${jetbrainsMono.variable}`}>
<body className={inter.className}>{children}</body>
</html>
);
}
For licensed brand fonts stored inside your repository, use localFont from next/font/local and point to .woff2 files.
// app/fonts.ts
import localFont from 'next/font/local';
export const brandSans = localFont({
src: [
{
path: '../public/fonts/BrandSans-Regular.woff2',
weight: '400',
style: 'normal',
},
{
path: '../public/fonts/BrandSans-Bold.woff2',
weight: '700',
style: 'normal',
},
],
variable: '--font-brand-sans',
});
Export the variable option from next/font and map --font-sans or --font-mono in your globals.css theme tokens. This lets utility classes like font-mono apply your self-hosted font cleanly across every component.
next/font inside every small component: Calling the font loader repeatedly creates duplicate font definitions; define fonts once in app/layout.tsx or app/fonts.ts.subsets option: Always specify subsets: ['latin'] (or the needed script) to keep font file payloads small.weight: Non-variable Google Fonts require an explicit weight array such as weight: ['400', '700'].next/font/google send user IP addresses to Google?No, Next.js downloads the font files once during next build and serves them from your own host, so browsers make zero requests to Google.
.woff2 or .ttf for next/font/local?Always prefer .woff2 files because they provide stronger compression and smaller downloads across all modern browsers.
Yes, export the secondary font from app/fonts.ts, import it inside that specific page or component, and apply secondaryFont.className.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
You've completed this section! Take a quick 5-question quiz to check your understanding.