Lee starts with a standard Next.js 13 app, which uses the system font stack. Adding Roboto through a Google Fonts link works, but the fallback font loads first and the app makes external requests for the font. On slow connections, this can mean a noticeable delay and layout shift.
Using Fonts in Next.js
Lee Robinson, a developer at Vercel, explains how to use Google Fonts and local fonts with Next.js 13, including Tailwind CSS.
Lee installs the separate `@next/font` package and imports Roboto in the Pages Router app. He sets the Latin subset and chooses weights, then applies the generated class to the page. Next.js self-hosts the font and adds a fallback font designed to reduce layout shift.
The same approach works with other Google Fonts, including Inter and IBM Plex. For variable fonts such as Inter, Lee recommends leaving out explicit weights where possible, which can improve performance.
Local font files work too, using a source path when defining the font. For Tailwind CSS, Lee applies a named CSS variable from the font and maps that variable to Tailwind’s sans family in the config. In the beta App Directory, he applies the font to the root layout.
Takeaways
- Use `@next/font` to self-host Google Fonts and avoid runtime requests to Google Fonts.
- Limit font subsets and weights to what the app needs.
- Consider variable fonts to reduce font file overhead.
- For Tailwind, expose the font through a CSS variable and map it in the Tailwind config.
- In the App Directory, apply the font in the root layout.
Mentioned in this talk
Search inside this talk
What was claimed 6
- feature
Next.js: Version 13 font optimization can self-host fonts and select a fallback font to reduce layout shift.
“automatically self-host the Roboto font on our Next.js server and figure out the fallback font”
YouTube Unverified1 source· Talk
- integrates with@next/font integrates with Google Fonts
“inside `_app` in the Pages directory, we can import a font from Google Fonts”
YouTube Unverified1 source· Talk
- uses@next/font uses Roboto
“I’m going to use Roboto”
YouTube Unverified1 source· Talk
- featureInter: Variable font; weights do not need to be specified.
“This is a variable font, so I don’t need to specify weights.”
YouTube Unverified1 source· Talk
- uses@next/font uses Inter
“Let’s use Inter”
YouTube Unverified1 source· Talk
- integrates with@next/font integrates with Tailwind CSS
“if you're using Tailwind CSS, you'll want to use a CSS variable instead”
YouTube Unverified1 source· Talk
Related talks
9:10
1:57:48
24:03
9:22
9:20
14:18