This drastically improves initial load times of albums. and also reduces bandwidth on mobile phones.
I implemented an POC that progressively enhances a page with this in such a way
that the page works the same with javascript enabled or disabled. Just when JS is enabled, the page loads faster
POC:
img.lazy {
display: hidden;
}
document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImage.classList.remove("lazy");
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
lazyImages.forEach(function(lazyImage) {
lazyImage.src = lazyImage.dataset.src;
lazyImage.classList.remove("lazy");
});
}
});
Using https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
This drastically improves initial load times of albums. and also reduces bandwidth on mobile phones.
I implemented an POC that progressively enhances a page with this in such a way that the page works the same with javascript enabled or disabled. Just when JS is enabled, the page loads faster
POC: