Slow-loading images can significantly impact your website's performance and user experience. Let's improve your portfolio's image loading with some proven techniques.
The Next.js Image component (next/image
) is designed specifically to optimize images and improve loading performance 1:
import Image from 'next/image'
// Instead of this:
// <img src="/profile.jpg" alt="Profile" width="400" height="300" />
// Use this:
<Image
src="/profile.jpg"
alt="Profile"
width={400}
height={300}
priority={true} // For above-the-fold images
/>
Always specify width and height attributes for your images to prevent layout shifts as images load 1:
<Image
src="/portfolio-image.jpg"
alt="Portfolio project"
width={800}
height={600}
/>
Next.js automatically serves WebP images which are significantly smaller than JPEG or PNG 12:
// No additional code needed - Next.js handles format conversion
<Image
src="/large-image.jpg" // Will be served as WebP if the browser supports it
alt="Large image"
width={1200}
height={800}
/>
For critical images visible in the initial viewport, use the priority
attribute 1:
<Image
src="/hero-image.jpg"
alt="Hero image"
width={1600}
height={900}
priority={true}
/>
For images below the fold, Next.js automatically implements lazy loading (loading only when they're about to enter the viewport) 2:
// No additional code needed - lazy loading is the default behavior
<Image
src="/below-fold-image.jpg"
alt="Below fold image"
width={600}
height={400}
/>
Use responsive sizing to serve appropriately sized images for different devices 2:
<Image
src="/responsive-image.jpg"
alt="Responsive image"
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
className="object-cover"
/>
Add placeholders while images load to improve perceived performance:
<Image
src="/portfolio-project.jpg"
alt="Portfolio project"
width={800}
height={600}
placeholder="blur"
blurDataURL="data:image/jpeg;base64,/9j..." // Base64 encoded tiny placeholder
/>
If you're loading images from external sources, configure allowed domains in your next.config.js
2:
// next.config.js
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'your-image-host.com',
},
],
},
}
Here's a complete example of an optimized image gallery component:
<link rel="preload">
in your document head for the most important images 1By implementing these optimizations, you should see a significant improvement in your portfolio's image loading performance, which will enhance both user experience and Core Web Vitals scores.