Next.js Image Optimization Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal Core Web Vitals.
import Image from 'next/image';
// Hero image with priority loading
export function HeroImage({ src, alt }: { src: string; alt: string }) {
return (
<Image
src={src}
alt={alt}
width={1200}
height={630}
priority
sizes="100vw"
className="rounded-xl object-cover"
/>
);
}
// Responsive thumbnail grid
export function ThumbnailGrid({ images }: { images: { src: string; alt: string }[] }) {
return (
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
{images.map((img, i) => (
<Image
key={i}
src={img.src}
alt={img.alt}
width={300}
height={300}
sizes="(max-width: 768px) 50vw, 25vw"
className="rounded-lg object-cover aspect-square"
loading="lazy"
/>
))}
</div>
);
}Use Cases
- Hero images
- Product galleries
- Blog featured images
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Image Lazy Load Component
Lazy load images with a blur-up placeholder effect using Intersection Observer and CSS transitions.
EXISTS vs IN Subquery Patterns
Choose between EXISTS and IN subqueries for optimal performance based on data distribution.
Covering Index (INCLUDE Columns)
Create covering indexes with INCLUDE columns to satisfy queries entirely from the index.
Partial Prerendering with Suspense
Combine static shells with streamed dynamic content using React Suspense for instant page loads.