Frontend School

Optimized Product Listing: Lazy Image Loading

Machine Coding
medium

Optimized Product Listing: Lazy Image Loading

Medium
React
JavaScript
Frontend
Lazy Loading
Performance
Intersection Observer

Overview

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.

What you need to do / Task

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.

  1. Display Product Cards: Render a list of product cards, each showing a product image, name, and price.
  2. Implement Lazy Loading: Images should only load their 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.
  3. Placeholder/Loading State: While an image is not yet loaded (either off-screen or in the process of loading), display a distinct placeholder or loading indicator.
  4. Smooth Transition (Optional but recommended): Implement a subtle fade-in effect or transition once an image successfully loads to provide a smoother user experience.

UI/UX expectations

  • The products should be displayed in a responsive grid or flex layout.
  • Each product card should have a consistent size and clear visual separation.
  • Initially, only the images visible in the viewport should begin loading. All other images should show their placeholder.
  • As the user scrolls down, images should smoothly transition from their placeholder state to the loaded image as they enter the viewport area.
  • No noticeable layout shifts should occur when an image loads.

Acceptance criteria

  • Only images that are initially visible in the viewport (or slightly offset, as configured by rootMargin) are loaded on page render.
  • As the user scrolls, new images are triggered to load only when they approach or enter the viewport.
  • A clear visual placeholder is displayed for images that have not yet loaded.
  • Images load successfully and are displayed correctly once loaded.
  • The application remains performant during scrolling, without jank or excessive resource usage.
  • The IntersectionObserver API is correctly utilized for detecting element visibility.

Constraints

Constraints & assumptions

  • You must use React for the UI development.
  • The IntersectionObserver API is the primary mechanism for lazy loading; avoid relying solely on the native loading="lazy" attribute for this problem.
  • A reasonable number of mock products (e.g., 20-30) will be provided in the starter code.
  • Image URLs will point to placeholder images (e.g., via.placeholder.com) which simulate real image loading.
  • Focus on the frontend implementation; no backend API calls or complex state management beyond image loading are required.

Out of scope

  • Advanced image optimization techniques like responsive images (srcset, sizes), different image formats (WebP, AVIF).
  • Server-side rendering (SSR) or Static Site Generation (SSG).
  • Complex error handling for image loading failures.

What interviewers evaluate

  • Correctness: Accurate implementation of IntersectionObserver for lazy loading.
  • Code Quality: Clean, readable, and modular React component design.
  • Performance: Efficient handling of image loading states and minimizing re-renders.
  • User Experience: Smooth visual transitions and responsiveness during interaction.

Loading editor...