Frontend School

Myntra Product Catalog: Lazy Image Loader

Machine Coding
medium

Myntra Product Catalog: Lazy Image Loader

Medium
React
JavaScript
Front-end
Performance
Lazy Loading
Intersection Observer

Overview

Myntra's product catalog pages often display a large number of items, each with one or more images. Loading all these images upfront can significantly degrade page load performance and user experience, especially on slower networks or devices. To combat this, implementing an efficient lazy loading mechanism for product images is crucial. This ensures that images are only fetched and rendered when they are about to become visible in the user's viewport, optimizing resource utilization and perceived performance.

What you need to do / Task

Your task is to build a React application that displays a list of product cards. Each product card will feature an image and some basic product information. The core requirement is to implement lazy loading for all product images.

  1. Display Product List: Render a scrollable list of product cards using the provided mock data.
  2. Implement Lazy Loading: Utilize the native Intersection Observer API to detect when a product image enters the viewport.
  3. Load Images On Demand: When an image is observed to be intersecting the viewport, load its high-resolution src attribute.
  4. Placeholder Display: Before an image is loaded, display a visible placeholder (e.g., a solid color block, a low-res preview, or a simple spinner) within the image area.
  5. Prevent Reloads: Ensure that once an image has been loaded, it does not attempt to reload on subsequent scrolls or re-intersections.

UI/UX expectations

  • The application should present a vertically scrollable grid or list of product cards.
  • Each product card should have a clearly defined area for the image at the top, followed by product name and brand.
  • The placeholder should be distinctly visible before the actual image appears.
  • Image loading should be smooth, without noticeable jank or significant layout shifts as images appear.
  • The overall layout should be responsive enough to adapt to different screen widths (e.g., using CSS Grid).

Acceptance criteria

  • On initial page load, images outside the visible viewport display their respective placeholders.
  • As the user scrolls down, images entering the viewport transition from placeholder to their actual high-resolution content.
  • Images that have already been loaded remain visible and do not attempt to reload when scrolled out of and back into view.
  • A visual placeholder is consistently shown for images that are not yet loaded.
  • The application remains performant during scrolling and image loading, with no noticeable UI freezes or excessive network requests.

Constraints

Constraints & assumptions

  • You must use React for building the user interface.
  • The solution should be implemented using functional components and React Hooks.
  • You are required to use the native Intersection Observer API for lazy loading; external libraries specifically for lazy loading are not permitted.
  • Mock product data will be provided in the starter code.
  • Focus on modern browser compatibility (browsers supporting Intersection Observer).

Out of scope

  • Implementing a full-fledged product details page or complex routing.
  • Advanced image optimization techniques beyond lazy loading (e.g., srcset, picture element, different image formats).
  • Server-side rendering (SSR) or static site generation (SSG).

What interviewers evaluate

  • Correct and efficient implementation of the Intersection Observer API.
  • Effective state management for handling image loading lifecycle within React components.
  • Clean, modular, and readable component structure, demonstrating good React practices.
  • Attention to performance and user experience, ensuring smooth image loading and scrolling.

Loading editor...