1.Basic Usage of the <Image> Component
The <Image> component in Next.js requires src, width, height, and alt attributes. Here’s a simple example:
import Image from 'next/image';
const HomePage = () => (
<div>
<Image
src="/images/my-image.jpg" // Path to your image
alt="Description of image" // Important for accessibility and SEO
width={500} // Specify width
height={300} // Specify height
/>
</div>
);
export default HomePage;
2. Advantages of Using <Image> Component
- Automatic Resizing: We can specify the image size, and Next.js will automatically resize the image.
- Lazy Loading: Images are lazy-loaded by default, meaning they will only load when they are in the viewport, reducing initial load times.
- Responsive Images: The
<Image>component generates responsive versions of the image (based on device width) and serves the most appropriate size. - Optimized Formats: Images are served in modern formats like WebP automatically, if supported by the browser.
- Caching: Next.js optimizes image caching for better performance.