Image Component in Next.js

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

The Next.js Image component extends the HTML <img> element with automatic optimization:
Size Optimization: Automatically serves correctly sized images for each device using modern formats like WebP
Visual Stability: Prevents layout shift when images load
Faster Page Loads: Images only load when they enter the viewport (lazy loading)
Asset Flexibility: On-demand resizing works with remote images too
import Image from "next/image";
For local images, import the image file directly:
import Image from "next/image";
import profilePic from "./me.png";
export default function Page() {
return (
<Image
src={profilePic}
alt="Picture of the author"
// width and height are automatically provided
// blurDataURL is automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
);
}
Next.js automatically determines the width and height from the imported file, which helps prevent layout shift.
For remote images, provide the URL as a string and manually specify width and height:
import Image from "next/image";
export default function Page() {
return (
<Image
src="https://s3.amazonaws.com/my-bucket/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
);
}
For security, you must define allowed remote image sources in your Next.js config:
// next.config.js
module.exports = {
images: {
remotePatterns: [
{
protocol: "https",
hostname: "s3.amazonaws.com",
pathname: "/my-bucket/**",
},
],
},
};
This is important: You should add the priority property to the image that will be the Largest Contentful Paint (LCP) element on each page.
<Image src={heroImage} alt="Hero image" priority />
The priority property does two important things:
Disables lazy loading for the image
Preloads the image
Many developers overlook this and let their LCP image load lazily by default, which reduces performance. When you run next dev, you'll get a console warning if your LCP image lacks the priority attribute.
Without priority, the LCP image only begins loading when it appears on the screen, delaying this important performance metric. With priority, the image loads right away when the page loads.
To prevent layout shift, images must be sized in one of three ways:
Automatically: Using a static import
Manually: Specifying width and height
Fill: Using the fill property to expand to parent element
If you don't know your image sizes:
Use fill and style the parent container
Normalize your images to standard sizes
Modify your API calls to return image dimensions
Style the Image component using:
className prop (recommended for most cases)
style prop for inline styles
When using fill:
Parent element must have position: relative
Parent element must have display: block
<Image
src={mountains}
alt="Mountains"
sizes="100vw"
style={{ width: "100%", height: "auto" }}
/>
<div style={{ position: "relative", height: "400px" }}>
<Image
src={mountains}
alt="Mountains"
fill
sizes="(min-width: 808px) 50vw, 100vw"
style={{ objectFit: "cover" }}
/>
</div>
<Image
src={mountains}
alt="Mountains"
placeholder="blur"
quality={100}
fill
sizes="100vw"
style={{ objectFit: "cover" }}
/>
You can configure the Image component and optimization API in your next.config.js file to:
Enable remote images
Define custom breakpoints
Change caching behavior
And more