Font Optimization
Self-host Google and local fonts with Next.js next/font, prevent layout shift and use CSS variables with Tailwind, with clear App Router examples.
On this page
1. What is font optimization?
It has two parts:
next/font/google: any Google Font, but the browser never sends a request to Google.next/font/local: your own.woff2files.
2. Why does it matter?
The usual way loads Google Fonts with a link tag. The browser reads the HTML first, then the CSS, then the font file. Until the font arrives, the text shows in a fallback font and then suddenly changes.
| Google Fonts with a link tag | next/font |
|---|---|
| An extra request from the browser to Google's server | The font is served from your own domain |
| Text changes with a jolt when the font arrives | A fallback with size-adjust, so less layout shift |
| The user's IP address goes to Google | No third-party request |
| All weights and subsets load | Only the subsets and weights you choose |
3. Where is it used?
| Situation | Approach |
|---|---|
| Main font for the whole site | next/font/google in the root layout |
| A paid custom brand font | next/font/local |
| Different fonts for headings and body | Two fonts, each with a CSS variable |
| Monospace for code blocks | Geist_Mono or JetBrains_Mono with a variable |
4. How does it work?
The font function is called at the top level of a module. The object it returns has className, style and (if you passed variable) variable. You apply these to html or to any element.
5. Code example
Google font in the root layout
import { Inter, Roboto_Mono } from "next/font/google";
import "./globals.css";
const inter = Inter({
subsets: ["latin"],
display: "swap",
variable: "--font-inter",
});
const robotoMono = Roboto_Mono({
subsets: ["latin"],
variable: "--font-roboto-mono",
});
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className={`${inter.variable} ${robotoMono.variable}`}>
<body className="font-sans">{children}</body>
</html>
);
}Connecting the variable in Tailwind v4
@import "tailwindcss";
@theme inline {
--font-sans: var(--font-inter);
--font-mono: var(--font-roboto-mono);
}Local font
import localFont from "next/font/local";
const brand = localFont({
src: [
{ path: "./fonts/Brand-Regular.woff2", weight: "400", style: "normal" },
{ path: "./fonts/Brand-Bold.woff2", weight: "700", style: "normal" },
],
variable: "--font-brand",
});
export default function Heading({ text }: { text: string }) {
return <h1 className={brand.className}>{text}</h1>;
}How does the code work?
| Item | Meaning |
|---|---|
Inter(...) | A Google font loader, use an underscore if the name has a space (Roboto_Mono) |
subsets | Only the needed characters (like latin) are preloaded |
variable | Creates a CSS variable you can use in Tailwind or CSS |
display: "swap" | Fallback text shows until the font arrives (this is also the default) |
@theme inline | Connects Tailwind's font-sans utility to the font variable |
localFont | Your own files, with the src path relative to the font loader file |
6. Common mistakes
Mistake: Calling the font function inside a component
Why? Font loaders must be called in module scope, otherwise a new instance is created on every render and you get a build error.
Better: Define the font in a const at the top of the file and use only className or variable in the component.
Mistake: Loading the same font separately on every page
Why? Several instances of the same font are created and extra CSS and font files are loaded.
Better: Define the font in one shared file like app/fonts.ts or apply it in the root layout, then import it from there.
Mistake: Keeping the Google Fonts link tag in the layout as well
Why? The font loads twice and the third-party request comes back, which removes the benefit of next/font.
Better: Remove the old link tag and use only next/font.
Interview tip
"How does next/font reduce CLS?" Answer: it automatically sets properties like size-adjust on the fallback font so that the fallback and the real font are almost the same size, which means the text does not move when the font swaps.
7. What to learn before and after
| Stage | Chapters |
|---|---|
| Before | Layouts & Templates, Styling: CSS Modules & Tailwind |
| Alongside | Image Optimization |
| After | Metadata & SEO, Performance & Core Web Vitals |
Final quick revision
| Point | Detail |
|---|---|
| Google fonts | next/font/google, self-hosted at build time |
| Your own files | next/font/local and src |
| With Tailwind | The variable option and @theme inline |
| Where to define | Module scope, often the root layout |
| Benefit | No external request, less CLS |