Menu

Loading Fonts with next/font

Lesson 3: Loading Fonts with next/font

What does next/font do?

next/font downloads font files at build time and serves them from your own domain.

  • No extra request to Google Fonts from the browser, which helps privacy and speed
  • No layout shift, because fallback fonts are adjusted to match the real font's size
  • Only the subsets and weights you request are included

Example

// app/layout.tsx
import { Inter, Merriweather } from 'next/font/google';

const inter = Inter({ subsets: ['latin'], variable: '--font-inter' });
const merriweather = Merriweather({
  subsets: ['latin'],
  weight: ['400', '700'],
  variable: '--font-serif',
});

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" className={`${inter.variable} ${merriweather.variable}`}>
      <body className="font-sans">{children}</body>
    </html>
  );
}

Local font files:

import localFont from 'next/font/local';

const brand = localFont({ src: './fonts/LearnHubSans.woff2', variable: '--font-brand' });

How it works

Option Purpose
subsets Character sets to include, such as latin or devanagari
weight Required for non-variable fonts
variable Creates a CSS variable for use in CSS or Tailwind
display Font-display strategy; defaults to swap

Variable fonts (like Inter) include all weights in one file, so weight is optional.

Common mistakes

Mistake Effect Fix
Calling the font loader inside a component Font reloaded per render, build error Call it at module level
Adding Google Fonts <link> tags manually Extra request, layout shift Use next/font/google
Loading many weights "just in case" Larger downloads Request only weights you use

Key takeaways

  • next/font self-hosts fonts and prevents layout shift automatically.
  • Use CSS variables to combine several fonts with Tailwind or CSS Modules.

Try it: Use Inter for body text and a serif font for headings on LearnHub, applied through CSS variables.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details