next/image Componentnext/image.WEBP or AVIF when the browser supports them.priority).Static Imports (at the top of the file):
import myImage from './path/to/image.jpg';
Dynamic Imports (using public folder):
<Image src="/images/dynamic.jpg" alt="Dynamic" width={500} height={300} />
| Prop | Description |
|---|---|
src | Source of the image. Must be statically analyzable or from the public folder. |
alt | Required. Describes the image for accessibility and SEO. |
width | Required. Width of the image in pixels. |
height | Required. Height of the image in pixels. |
sizes | Specifies responsive sizes e.g., "(max-width: 768px) 100vw, 50vw" |
priority | Forces the image to load immediately (adds fetchpriority="high"). |
placeholder | 'blur' or 'empty' for loading UX. 'blur' uses a blurred low-res version. |
quality | Sets image quality (1–100). Default is 75. |
next.config.js):
module.exports = { images: { domains: ['example.com', 'cdn.myimages.com'], }, };
// next.config.js module.exports = { images: { loaders: [ { name: 'custom-loader', path: 'https://res.cloudinary.com/my-account/image/upload', }, ], }, }; // In component <Image src="my-image.jpg" loader={({ src }) => `/custom-loader?url=${src}`} alt="Cloudinary Image" width={500} height={300} />
next/image over a standard <img> tag?Answer:
The
next/imagecomponent provides automatic optimization (resizing, format conversion, compression) and ensures correct loading behavior (lazy-loading, priority hints). It also enforces accessibility best practices by requiringalttext and prevents layout shift by requiring explicitwidth/height. Additionally, it integrates seamlessly with Next.js's static optimization and supports responsive images via thesizesprop.
next/image handles responsive images.Answer:
When you provide the
sizesprop, Next.js generates multiple image sizes (based on the device pixel ratio) and includes asrcsetattribute. The browser then selects the optimal image based on viewport size and device capabilities. For example:<Image src="/image.jpg" alt="Responsive" width={500} height={300} sizes="(max-width: 768px) 100vw, 50vw" />This ensures the correct image size is loaded for mobile vs. desktop, reducing bandwidth usage.
Answer:
By default, Next.js only optimizes images from the same origin. To enable external images:
- Add the domain to
domainsinnext.config.js:// next.config.js module.exports = { images: { domains: ['example.com'], }, };- Ensure CORS headers are correctly configured on the external server.
- Alternatively, use a custom loader for CDNs (e.g., Cloudinary).
placeholder="blur" and placeholder="empty"?Answer:
"blur": Next.js generates a low-resolution blurred placeholder (a.k.a. "blur-up") from the image’s dominant colors. This provides visual context while the full image loads."empty": Shows a blank (transparent) area until the image loads. Useful for solid-background images where a blur might not be helpful.
priority prop?Answer:
Use
priorityfor above-the-fold images that are visible during the initial load (e.g., hero banners, logos). It:
- Prefetches the image.
- Adds
fetchpriority="high".- Disables lazy-loading.
- Ensures the image is inlined if possible.
import heroImage from './hero.jpg'; export default function Home() { return ( <div> <Image src={heroImage} alt="Hero Section" width={1200} // Actual width of the image height={800} // Actual height of the image priority // Load immediately placeholder="blur" /> </div> ); }
// next.config.js module.exports = { images: { domains: [' resaleapi.com'], }, };
// Component <Image src="https://resaleapi.com/images/product.jpg" alt="Product" width={600} height={400} sizes="(max-width: 600px) 100vw, 600px" />
Hands-On Practice:
next/image in both Pages Router and App Router.next/image.Configuration Deep Dive:
next.config.js options: deviceSizes, imageSizes, domains, and custom loaders.Performance Metrics:
Edge Cases:
next/image handle SVGs? (Requires next/image v12+ and width/height.)width/height are mismatched? (Next.js will attempt to infer but may cause layout shift.)Stay Updated:
💡 Need clarification or want to simulate a mock interview? Ask me any question!
Start a new session to explore different topics or increase the difficulty level.
Start New Session