Load fonts with next/font to avoid layout shift

next/font downloads and self-hosts Google or local fonts at build time, generates size-adjust fallbacks and removes the render-blocking stylesheet request. Import the font in the layout, pass its className to the html element, and reference the CSS variable in styles. Verify with a throttled trace that text does not shift and that no font request leaves your origin.

mediumIn codeimpact effort

Why this matters

Self-hosted fonts remove the third-party request and the jump.

Get the full breakdown

The extended brief explains when this works, when it does not, and the trade-offs. Join the waitlist and it unlocks for every tip when sign-in opens; MCP access is the paid part.

Join the waitlist

Before you start

  • Next.js project with a root layout
  • The font family, weights and subsets the design uses

How to apply it

  1. Import the font in the layout

    Import the font from next/font/google or next/font/local in app/layout.tsx, pass subsets and weights, and assign the returned className to the html element.

    Check: The rendered html element carries the generated font class and no link tag points at fonts.googleapis.com.

  2. Reference it through a CSS variable

    Set variable to a name such as --font-sans and use that variable in the global stylesheet so components inherit the same family without importing it again.

    Check: grep -n 'font-family' app/globals.css shows the variable, and the built CSS contains no external font URL.

  3. Measure the swap

    Run a throttled mobile trace with the network cache disabled and record layout shift around the first text paint. Compare it with the previous build.

    Check: Lighthouse reports CLS below 0.1 on the typography-heavy template.

  4. Trim weights and subsets

    Load only the weights and subsets the design uses, then re-check the font files served. Remove unused families before deploying.

    Check: The network panel shows two font files at most for the primary family.

Get the prompt for your agent

A ready-to-paste prompt for your agent that implements this tip and verifies the result in your repo.

Join the waitlist

Source: seo-mcp playbook · curated entry · report an error

Framework notes

nextjs-app

Import fonts with `next/font/google` or `next/font/local` in `app/layout.tsx`, pass the `className` to `<html>`, and expose a `variable` for the stylesheet.

Official references

Let your agent apply this + 161 more tips.

Pre-order — $19