Performance: Image optimization with next/image
This affects page load speed by reducing image file size and optimizing delivery, improving Largest Contentful Paint (LCP).
import Image from 'next/image'; export default function MyComponent() { return <Image src="/photo.jpg" alt="Sample photo" width={800} height={600} />; }
<img src="/photo.jpg" alt="Sample photo" width="800" height="600" />
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Standard <img> tag | Minimal | 1 reflow per image load | High paint cost for large images | [X] Bad |
| next/image component | Minimal | Single reflow with optimized image | Lower paint cost due to smaller images | [OK] Good |
next/image in a Next.js project?next/image component is designed to optimize images automatically, improving loading speed and quality.Image component from Next.js for image optimization?import Image from 'next/image';.Image component requires src, alt, and explicit width and height for optimization. import Image from 'next/image';import Image from 'next/image';
export default function Logo() {
return (
<Image src='/logo.png' alt='Company Logo' width={200} height={100} priority />
);
}priority prop forces the image to load immediately instead of lazy loading.import Image from 'next/image';
export default function Profile() {
return (
<Image src='/profile.jpg' alt='User Profile' width='150' height='150' />
);
}Image requires width and height as numbers, not strings. Here they are strings ('150').width={150} and height={150}.next/image. Which approach correctly uses the sizes attribute to serve appropriate image sizes on different screen widths?sizes attribute defines image display sizes for different viewport widths using media queries.<Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' /> correctly uses (max-width: 600px) 100vw, 50vw meaning full viewport width on small screens, half on larger screens. This is a common responsive pattern.Pass the test and this pattern moves unknown