🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js Images and Fonts

next/image resizes each picture for the reader. next/font loads the font from your own server.

The cover in the gallery

Image takes the file, the width, the height, and the alt text. The reader gets the small file.

<Image src="/covers/dune.png" width={400} height={600} alt="Cover of Dune by Frank Herbert" />

The font in the layout

Geist loads from next/font/google. The layout uses the font through a CSS variable.

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

const geistSans = Geist({
  variable: '--font-geist-sans',
  subsets: ['latin'],
});

Example

curl -s -o /dev/null -w "plain request: %{size_download} bytes\n" "http://localhost:3110/_next/image?url=%2Fcovers%2Fdune.png&w=640&q=75"
curl -s -H "Accept: image/webp" -o /dev/null -w "webp request: %{size_download} bytes\n" "http://localhost:3110/_next/image?url=%2Fcovers%2Fdune.png&w=640&q=75"

Key points

  • Image needs a width and a height. The cover is 400 wide and 600 tall.
  • The plain request returned 2,645 bytes. The file on disk is 7,292 bytes.
  • A browser that accepts WebP gets a WebP file. That file is 3,814 bytes.
  • The font comes from next/font/google. The font files ship with the app.
  • A plain img tag sends the full file every time. Image sends the small file.

In this example, two requests ask for the same cover. The plain request gets 2,645 bytes, down from 7,292 on disk. The WebP request gets a real WebP file of 3,814 bytes. The WebP file is bigger here, because the source is a small flat-colour picture. The win is the drop from 7,292 to 2,645. The format is a bonus, not the whole win.

Note

Give every Image an alt text. It describes the picture to readers who cannot see it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

What does next/image change about a picture?