Nextjs Fundamentals · Optimization & Metadata

Self-hosting fonts

`next/font`

Self-hosting fonts

next/font self-hosts your fonts at build time. It removes the external network request to a font provider, improves privacy and performance, and eliminates layout shift.

For a Google font, import it from next/font/google, call it with options, and apply the returned className:

import { Geist } from 'next/font/google';

const geist = Geist({ subsets: ['latin'] });

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" className={geist.className}>
      <body>{children}</body>
    </html>
  );
}

The font file is downloaded and served from your own domain, so the browser never contacts Google. Next.js also generates a matching fallback font with adjusted metrics, so text does not jump when the real font loads.

For a font file you already have, import localFont from next/font/local and point src at the file:

import localFont from 'next/font/local';

const myFont = localFont({ src: './my-font.woff2' });

Add the font in the root layout to apply it across the whole app.

In short: next/font self-hosts fonts from Google or a local file, so there are no external requests and no layout shift.

You are about to answer a short quiz on next/font.

  1. next/font self-hosts fonts so the browser makes no request to Google.
  2. To use a local font file, import localFont from ___.
  3. next/font generates an adjusted fallback font to prevent layout shift.
  4. You apply a loaded font to an element with the returned ___ property.
  5. next/font requires a runtime request to the font provider on each page load.