More Next.js answers

by Danny
Published 2 min read

Use next/font/google. It downloads the font at build time, serves it from your own domain and creates a size-matched fallback so text does not jump when the font loads.

The short answer

#

Do not add a <link> to fonts.googleapis.com. Import the font from next/font/google, call it once at module level, and apply its class or CSS variable in your root layout. Next.js self-hosts the files and generates a fallback font with matching metrics, so the page does not shift when the real font arrives.


_12
// app/layout.tsx
_12
import { Inter } from 'next/font/google';
_12
_12
const inter = Inter({ subsets: ['latin'], display: 'swap' });
_12
_12
export default function RootLayout({ children }: { children: React.ReactNode }) {
_12
return (
_12
<html lang="en" className={inter.className}>
_12
<body>{children}</body>
_12
</html>
_12
);
_12
}

Why it happens

#

With a normal web font, the browser first paints text in a fallback such as Arial, then swaps in the web font once it downloads. The two fonts have different widths and line heights, so lines rewrap, headings grow or shrink, and content below moves. That movement counts towards Cumulative Layout Shift (CLS), one of the Core Web Vitals. Loading the CSS from Google adds another delay, because the browser has to connect to two extra domains before it even knows which font files to fetch.

next/font fixes both problems:

  • The CSS and font files are downloaded at build time and served with your other static assets. The browser sends no requests to Google.
  • The font is preloaded on the routes that use it.
  • With adjustFontFallback (on by default), Next.js writes a fallback @font-face rule with size adjustments so the fallback takes up almost the same space as the real font. When the swap happens, very little moves.

How to fix it

#

Use a variable font where you can

Variable fonts such as Inter, Roboto Flex or Geist need no weight option and cover every weight in one file. For a font that is not variable, list the weights you use.


_10
import { Roboto } from 'next/font/google';
_10
_10
const roboto = Roboto({
_10
weight: ['400', '700'],
_10
style: ['normal', 'italic'],
_10
subsets: ['latin'],
_10
});

Font names with spaces use underscores in the import: Roboto_Mono, Source_Sans_3.

Expose fonts as CSS variables

When you use more than one font, or style with Tailwind, set the variable option and put the variables on <html>.


_10
// app/fonts.ts
_10
import { Inter, JetBrains_Mono } from 'next/font/google';
_10
_10
export const sans = Inter({ subsets: ['latin'], variable: '--font-inter' });
_10
export const mono = JetBrains_Mono({ subsets: ['latin'], variable: '--font-jetbrains-mono' });


_10
// app/layout.tsx
_10
import { sans, mono } from './fonts';
_10
_10
export default function RootLayout({ children }: { children: React.ReactNode }) {
_10
return (
_10
<html lang="en" className={`${sans.variable} ${mono.variable}`}>
_10
<body>{children}</body>
_10
</html>
_10
);
_10
}

Then use the variables in CSS. With Tailwind CSS v4, map them in @theme inline:


_10
@import 'tailwindcss';
_10
_10
@theme inline {
_10
--font-sans: var(--font-inter);
_10
--font-mono: var(--font-jetbrains-mono);
_10
}

Give the next/font variables their own names, such as --font-inter, rather than reusing Tailwind's --font-sans, so the theme token never points at itself. The font-sans and font-mono utilities now use your fonts, and Tailwind's base styles apply font-sans to the page by default.

Local font files

For a licensed or custom font, use next/font/local with a path relative to the file that calls it.


_10
import localFont from 'next/font/local';
_10
_10
export const brand = localFont({
_10
src: './fonts/Brand-Variable.woff2',
_10
variable: '--font-brand',
_10
});

Choose display: optional for zero swap

The default display value is swap. If you would rather never see a swap, display: 'optional' lets the browser keep the fallback for that page view when the font is slow. The font is cached for the next page.

Common pitfalls

#
  • Calling the font function in many files. Each call creates a separate font instance. Define fonts once in a shared file such as app/fonts.ts and import the objects.
  • Calling it inside a component. Font loaders must be called at module scope and assigned to a constant.
  • Forgetting subsets. With preloading on, Next.js warns if you do not say which subsets to preload. ['latin'] is the usual choice.
  • Keeping the old link tag. If a Google Fonts <link> or @import is still in your CSS, you download the font twice and lose the benefit.
  • Turning off adjustFontFallback. Without it, the fallback has no size adjustments and the shift comes back.
  • Loading too many families. Each family and weight is another download. Two families is plenty for most sites.
#

Was this resource helpful?