Fullstack CourseLearn by building
Back to week 3

Topic

Optimize fonts

Definition

next/font self-hosts and optimizes supported font files so pages can avoid a third-party font request and reduce layout shift.

In simpler words

Load the project font through Next so text arrives predictably without asking another site for it.

Typography affects readability and performance because late font swaps can move content after users begin reading.

After this you can

  • Load a font with next/font
  • Apply its class or variable at a layout boundary
  • Avoid shipping unused font weights

Configure fonts once

A font definition is usually created near the root layout and applied through its returned className or CSS variable. This keeps type choices consistent across nested routes.

Request only the weights and styles the design uses. Every additional font file competes with application code, images, and data for network and rendering work.

Apply a font at the root

import { IBM_Plex_Sans } from 'next/font/google';
const plex = IBM_Plex_Sans({ subsets: ['latin'], weight: ['400', '600'] });

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

The layout gives descendant routes a consistent font class.

Mistake: importing a font with CSS @import

/* Wrong — extra third-party request and less control */
@import url('https://fonts.example/font.css');

// Right — configure the font with next/font in the layout

next/font manages font assets as part of the application build.

Make typography a system

Definition

A font choice needs a fallback stack, sensible weight range, and readable line lengths—not just a downloaded file.

In simpler words

Define reusable typography tokens so a heading does not gain a different font rule on every route.

Keep the Course’s IBM Plex typography intentional and consistent.

Check that bold, italic, and numeric data use the weights the application actually loads.

Live playground

Font optimization playground

Apply a returned font class and compare it with an external CSS import.

Configure next/font once near the root layout.

next-font-optimization

Keep in mind

  • Configure fonts near the root layout.
  • Load only weights that the UI uses.
  • Use font variables when the styling system needs a shared token.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement is the most accurate definition of Next font optimization?
Syntax2. Which code-level choice is consistent with Next font optimization?
Practical3. A reviewer sees this situation. Which decision best applies Next font optimization?
Logic4. What reasoning correctly explains why this approach works for Next font optimization?
Concept5. Which boundary does the correct use of Next font optimization preserve?
Practical6. What is the safest next step when implementing Next font optimization?
Syntax7. Which implementation direction matches the rule for Next font optimization?
Logic8. Which consequence follows from applying Next font optimization correctly?
Concept9. Which claim about Next font optimization is true in this monorepo?
Practical10. Which team practice best demonstrates Next font optimization?