Implementing lazy image loading with IntersectionObserver
Why the change?
Instead of requesting every image at once, including off-screen ones, images now load as they enter the viewport.
How was it implemented?
IntersectionObserver detects when an image enters the viewport. The observing logic lives in a useLazyImage hook, and the observer is disconnected when the component unmounts.
TypeScript · cleanup
useEffect(() => {
observer.observe(image);
return () => {
observer.disconnect();
};
}, [image]);What should you check?
Check that images appear correctly while scrolling and that observers are cleaned up once they leave the viewport. Actual performance gains need to be measured separately.
This draft was written by AI. Review the code and explanations before publishing.