The component addresses layout shift, images that are too large for the visitor’s screen or device, and the difficulty of getting images to display as intended. Importing a local image supplies its dimensions automatically, which helps prevent layout shift.
Using Images in Next.js
Lee Robinson, a developer at Vercel, walks through practical Next.js Image component examples in a local demo.
For a local PNG that is 30.3 KB, the Next.js image route serves an optimized 7.2 KB version in AVIF or WebP in the demo. Remote images also work, but the config must allowlist their domains and URL patterns. Remote images need explicit width and height to prevent layout shift.
The `fill` prop makes an image occupy its parent instead of using explicit dimensions, and its parent needs positioning and a defined size. With `fill`, the `sizes` prop helps Next.js choose suitable image variations for the viewport. The examples also cover quality, imported-image blur placeholders, and CSS such as `object-fit: cover`.
A CSS Grid with relatively positioned, 400-pixel-tall parents, `fill`, and `object-fit: cover` creates a responsive layout for multiple images. The component also responds as the viewport changes. Developers can configure a loader function to build image URLs for another optimization service, using values such as source, width, and quality.
Takeaways
- Import local images to have Next.js infer dimensions and reduce layout shift.
- Allowlist remote image hosts and paths in the Next.js config.
- Pair `fill` with a positioned parent, a defined size, and an appropriate `sizes` value.
- Use `object-fit: cover` and CSS Grid for flexible image galleries.
- Configure a loader when you want image URLs generated for another optimization service.
Mentioned in this talk
Search inside this talk
What was claimed 8
- featureNext.js Image component: Automatically supplies dimensions for imported local images to help prevent layout shift
“importing an image automatically does that for you”
YouTube Unverified1 source· Talk
- featureNext.js Image component: Optimizes images and can serve AVIF or WebP formats
“The /_next/image route automatically optimizes that image”
YouTube Unverified1 source· Talk
- featureNext.js Image component: Remote image sources require an explicit domain allowlist
“we have to define an explicit allowlist of the domains”
YouTube Unverified1 source· Talk
- featureNext.js Image component: The fill prop makes the image take up the width of its parent
“we can do that by specifying the fill prop”
YouTube Unverified1 source· Talk
- featureNext.js Image component: Can generate a blur data URL placeholder for imported local images
“Next.js can automatically generate the blur data URL”
YouTube Unverified1 source· Talk
- featureNext.js Image component: The quality prop defaults to 75%
“It defaults to 75%, I believe”
YouTube Unverified1 source· Talk
- featureNext.js Image component: Images are responsive by default
“images are responsive by default”
YouTube Unverified1 source· Talk
- featureNext.js Image component: A loader function can define image URLs for a chosen optimization service
“you can specify a loader to make that easy”
YouTube Unverified1 source· Talk
Related talks
24:03
9:20
6:05
16:24
14:18
17:18