Implement lazy loading for gallery images to prevent loading 400+ images simultaneously #196
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When viewing an album with many images (e.g., 400 photos), the gallery page would attempt to load all images simultaneously, causing:
Solution
Implemented lazy loading using the IntersectionObserver API to load images only when they enter or are near the viewport. This spreads out network requests over time as users scroll through the gallery.
Changes
1. New
useInViewport
Hook (src/hooks/useInViewport.ts
)Created a reusable React hook that leverages IntersectionObserver to detect when elements enter the viewport:
2. Enhanced
ImageDisplay
ComponentAdded an optional
enabled
prop that controls when React Query should fetch the image:true
for backward compatibility3. Updated Album Page
Created a
LazyImageCard
wrapper component for gallery grid images:Results
Before: 400+ simultaneous requests when opening an album
After: Only ~10-20 initial requests, more load progressively as user scrolls
Performance Impact
Technical Details
IntersectionObserver
API (supported in all modern browsers)ImageDisplay
usageThis implementation directly addresses the issue while maintaining minimal, surgical changes to the codebase.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.