Skip to content
Styling & OptimizationChapter 27 / 34Beginner5 min read

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 .woff2 files.

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 tagnext/font
An extra request from the browser to Google's serverThe font is served from your own domain
Text changes with a jolt when the font arrivesA fallback with size-adjust, so less layout shift
The user's IP address goes to GoogleNo third-party request
All weights and subsets loadOnly the subsets and weights you choose

3. Where is it used?

SituationApproach
Main font for the whole sitenext/font/google in the root layout
A paid custom brand fontnext/font/local
Different fonts for headings and bodyTwo fonts, each with a CSS variable
Monospace for code blocksGeist_Mono or JetBrains_Mono with a variable

4. How does it work?

next/font flow
The font function is called in the layout
next build downloads the font files
The files are bundled with the static assets
A unique className or CSS variable is generated
The browser loads the font from your own domain

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

tsx
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

css
@import "tailwindcss";

@theme inline {
  --font-sans: var(--font-inter);
  --font-mono: var(--font-roboto-mono);
}

Local font

tsx
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?

ItemMeaning
Inter(...)A Google font loader, use an underscore if the name has a space (Roboto_Mono)
subsetsOnly the needed characters (like latin) are preloaded
variableCreates 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 inlineConnects Tailwind's font-sans utility to the font variable
localFontYour 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

Final quick revision

PointDetail
Google fontsnext/font/google, self-hosted at build time
Your own filesnext/font/local and src
With TailwindThe variable option and @theme inline
Where to defineModule scope, often the root layout
BenefitNo external request, less CLS