Use the Image Component from next/image
Instead of using a regular <img> tag, import and use the Image component:
1import Image from 'next/image';
2export default function Profile() {
3 return (
4 <Image
5 src="/images/profile.jpg" // Can also be a remote URL
6 alt="Profile photo"
7 width={300}
8 height={300}
9 />
10 );
11}
1import Image from 'next/image';
2export default function Profile() {
3 return (
4 <Image
5 src="/images/profile.jpg" // Can also be a remote URL
6 alt="Profile photo"
7 width={300}
8 height={300}
9 />
10 );
11}
Benefits of next/image:
- Automatic lazy loading: Images load only when they enter the viewport.
- Responsive loading: Next.js serves appropriately sized images based on device size and resolution.
- Optimized formats: Images are served in modern formats like WebP when supported.
- Blur-up placeholders: Adds a nice blur effect while the image loads.
Additional Optimization Features:
1. Responsive Layouts
You can make the image responsive by using the layout="responsive" prop (for older versions) or using the fill prop in newer versions with Tailwind or custom styles.
1<Image
2 src="/images/banner.jpg"
3 alt="Banner"
4 width={1200}
5 height={600}
6 style={{ width: '100%', height: 'auto' }}
7/>
1<Image
2 src="/images/banner.jpg"
3 alt="Banner"
4 width={1200}
5 height={600}
6 style={{ width: '100%', height: 'auto' }}
7/>
2. Remote Images (External URLs)
Add the domain to your next.config.js file:
1module.exports = {
2 images: {
3 domains: ['example.com'],
4 },
5};
1module.exports = {
2 images: {
3 domains: ['example.com'],
4 },
5};
3. Use Placeholder Blur (Progressive Loading)
1<Image
2 src="/images/photo.jpg"
3 alt="Photo"
4 width={500}
5 height={500}
6 placeholder="blur"
7 blurDataURL="/images/placeholder.jpg"
8/>
1<Image
2 src="/images/photo.jpg"
3 alt="Photo"
4 width={500}
5 height={500}
6 placeholder="blur"
7 blurDataURL="/images/placeholder.jpg"
8/>