mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
UX: Use dominant color as image loading placeholder (#18248)
We previously had a system which would generate a 10x10px preview of images and add their URLs in a data-small-upload attribute. The client would then use that as the background-image of the `<img>` element. This works reasonably well on fast connections, but on slower connections it can take a few seconds for the placeholders to appear. The act of loading the placeholders can also break or delay the loading of the 'real' images. This commit replaces the placeholder logic with a new approach. Instead of a 10x10px preview, we use imagemagick to calculate the average color of an image and store it in the database. The hex color value then added as a `data-dominant-color` attribute on the `<img>` element, and the client can use this as a `background-color` on the element while the real image is loading. That means no extra HTTP request is required, and so the placeholder color can appear instantly. Dominant color will be calculated: 1. When a new upload is created 2. During a post rebake, if the dominant color is missing from an upload, it will be calculated and stored 3. Every 15 minutes, 25 old upload records are fetched and their dominant color calculated and stored. (part of the existing PeriodicalUpdates job) Existing posts will continue to use the old 10x10px placeholder system until they are next rebaked
This commit is contained in:
@@ -1,14 +1,3 @@
|
||||
// Min size in pixels for consideration for lazy loading
|
||||
const MINIMUM_SIZE = 150;
|
||||
|
||||
function forEachImage(post, callback) {
|
||||
post.querySelectorAll("img").forEach((img) => {
|
||||
if (img.width >= MINIMUM_SIZE && img.height >= MINIMUM_SIZE) {
|
||||
callback(img);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function isLoaded(img) {
|
||||
// In Safari, img.complete sometimes returns true even when the image is not loaded.
|
||||
// naturalHeight seems to be a more reliable check
|
||||
@@ -16,40 +5,48 @@ function isLoaded(img) {
|
||||
}
|
||||
|
||||
export function nativeLazyLoading(api) {
|
||||
api.decorateCookedElement(
|
||||
(post) =>
|
||||
post.querySelectorAll("img").forEach((img) => (img.loading = "lazy")),
|
||||
{
|
||||
id: "discourse-lazy-load",
|
||||
}
|
||||
);
|
||||
|
||||
api.decorateCookedElement(
|
||||
(post) => {
|
||||
const siteSettings = api.container.lookup("service:site-settings");
|
||||
|
||||
forEachImage(post, (img) => {
|
||||
img.loading = "lazy";
|
||||
post.querySelectorAll("img").forEach((img) => {
|
||||
// Support for smallUpload should be maintained until Post::BAKED_VERSION is bumped higher than 2
|
||||
const { smallUpload, dominantColor } = img.dataset;
|
||||
|
||||
if (siteSettings.secure_media) {
|
||||
if (siteSettings.secure_media && smallUpload) {
|
||||
// Secure media requests go through the app. In topics with many images,
|
||||
// this makes it very easy to hit rate limiters. Skipping the low-res
|
||||
// placeholders reduces the chance of this problem occuring.
|
||||
return;
|
||||
}
|
||||
|
||||
if (img.dataset.smallUpload) {
|
||||
if (!isLoaded(img)) {
|
||||
if (!img.onload) {
|
||||
img.onload = () => {
|
||||
img.style.removeProperty("background-image");
|
||||
img.style.removeProperty("background-size");
|
||||
};
|
||||
}
|
||||
if ((smallUpload || dominantColor) && !isLoaded(img)) {
|
||||
if (!img.onload) {
|
||||
img.onload = () => {
|
||||
img.style.removeProperty("background-image");
|
||||
img.style.removeProperty("background-size");
|
||||
img.style.removeProperty("background-color");
|
||||
};
|
||||
}
|
||||
|
||||
img.style.setProperty(
|
||||
"background-image",
|
||||
`url(${img.dataset.smallUpload})`
|
||||
);
|
||||
if (smallUpload) {
|
||||
img.style.setProperty("background-image", `url(${smallUpload})`);
|
||||
img.style.setProperty("background-size", "cover");
|
||||
} else {
|
||||
img.style.setProperty("background-color", `#${dominantColor}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
{
|
||||
onlyStream: true,
|
||||
id: "discourse-lazy-load-after-adopt",
|
||||
afterAdopt: true,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user