Optimized Product Listing: Lazy Image Loading
Myntra's product listing pages (PLPs) feature a vast number of product images. Loading all these images upfront can significantly impact initial page load time and user experience, especially on slower networks or devices. Implementing an efficient lazy loading mechanism is crucial for performance optimization, ensuring that images are only loaded when they are relevant to the user's current view.
Your task is to build a React component that displays a gallery of product cards. Each product card will feature an image, name, and price. The core requirement is to implement lazy loading for all product images using the IntersectionObserver API.
src when they are near or within the user's viewport. Use IntersectionObserver to detect when an image element is intersecting with the root viewport.rootMargin) are loaded on page render.IntersectionObserver API is correctly utilized for detecting element visibility.Constraints
IntersectionObserver API is the primary mechanism for lazy loading; avoid relying solely on the native loading="lazy" attribute for this problem.via.placeholder.com) which simulate real image loading.srcset, sizes), different image formats (WebP, AVIF).IntersectionObserver for lazy loading.Loading editor...