Frontend School

Optimized Image Gallery with Lazy Loading

Machine Coding
medium

Optimized Image Gallery with Lazy Loading

Medium
React
JavaScript
Front-end
Performance
Lazy Loading
IntersectionObserver
UI/UX

Overview

In Ola's ecosystem, various services like 'Ola Cabs' or 'Ola Electric' often feature extensive galleries of vehicle images, driver profiles, or product showcases. Displaying a large number of high-resolution images can significantly impact page load times and user experience, especially on mobile devices or slower networks. This problem challenges you to build a performant image gallery that loads images only when they are about to become visible in the viewport, ensuring a smooth and responsive user experience.

What you need to do / Task

Your primary goal is to implement a performant image gallery that utilizes lazy loading to improve initial page load and overall user experience. Specifically, you must:

  1. Render an Image Gallery: Display a list of provided image data in a scrollable container.
  2. Implement Lazy Loading: Images should only begin loading when they are within a certain proximity (e.g., 200px) of the user's viewport, using native browser APIs.
  3. Display Placeholders: While an image is loading, a visual placeholder (e.g., a grey box or a simple spinner) should be shown.
  4. Handle Load Errors: If an image fails to load (e.g., due to a broken URL), it should display a clear error state (e.g., a broken image icon or fallback text).
  5. Maintain UI Responsiveness: Ensure that the gallery remains responsive and performant during scrolling, even with many images.

UI/UX expectations

  • Layout: The images should be displayed in a simple grid or list layout, allowing for vertical scrolling.
  • Placeholders: Before an image loads, a distinct placeholder area should occupy its space. This could be a solid background color (e.g., light grey) or a simple loading indicator.
  • Error State: For images that fail to load, replace the placeholder/image area with an indicator of failure (e.g., a broken image icon, or text like "Image failed to load").
  • Interactions: Scrolling should be smooth and free of jank. Images should appear seamlessly as they come into view.

Acceptance criteria

  • Only images within or very close to the initial viewport are loaded immediately on page render.
  • Images outside the viewport load progressively as the user scrolls them into view.
  • A visible placeholder is displayed for images that have not yet loaded.
  • Images that fail to load display an appropriate error state.
  • Scrolling the image gallery is smooth and does not exhibit noticeable performance bottlenecks.
  • The solution correctly leverages IntersectionObserver for lazy loading.

Constraints

Constraints & assumptions

  • You will be provided with a mock array of image data (URLs, alt text). Assume there could be many such images (e.g., 20-50).
  • The environment is a modern web browser (Chrome, Firefox, Safari).
  • You are expected to use React for building the UI.
  • You should implement the lazy loading mechanism using the browser's native IntersectionObserver API, without relying on third-party lazy-loading libraries (e.g., react-lazyload).
  • All images are expected to have a consistent aspect ratio, simplifying layout.

Out of scope

  • Implementing infinite scrolling (you only need to lazy load a fixed list).
  • Complex image optimization techniques (e.g., responsive images using srcset, different image formats).
  • Server-side rendering (SSR) or static site generation (SSG).
  • Advanced accessibility features beyond basic alt attributes.

What interviewers evaluate

  • Correctness and Efficiency: Proper application of IntersectionObserver for lazy loading and efficient state management.
  • Error Handling: Robust handling and clear visual feedback for image loading failures.
  • Code Quality: Clean, modular, and maintainable React component structure.
  • Performance: Smooth scrolling and minimal jank, demonstrating an understanding of UI performance.
  • Adherence to Requirements: All specified UI/UX and acceptance criteria are met.

Loading editor...