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:
- Render an Image Gallery: Display a list of provided image data in a scrollable container.
- 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.
- Display Placeholders: While an image is loading, a visual placeholder (e.g., a grey box or a simple spinner) should be shown.
- 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).
- 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.