From 04e433d286ddd37dbfdef9acad7a2dd425616ded Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 14 Sep 2022 12:42:59 +0100 Subject: [PATCH] PERF: Skip image placeholders when secure_media is enabled (#18247) 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. --- .../discourse/app/lib/lazy-load-images.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/lazy-load-images.js b/app/assets/javascripts/discourse/app/lib/lazy-load-images.js index 4f94667c15f..5b79ebe17dd 100644 --- a/app/assets/javascripts/discourse/app/lib/lazy-load-images.js +++ b/app/assets/javascripts/discourse/app/lib/lazy-load-images.js @@ -17,9 +17,19 @@ function isLoaded(img) { export function nativeLazyLoading(api) { api.decorateCookedElement( - (post) => + (post) => { + const siteSettings = api.container.lookup("service:site-settings"); + forEachImage(post, (img) => { img.loading = "lazy"; + + if (siteSettings.secure_media) { + // 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) { @@ -36,7 +46,8 @@ export function nativeLazyLoading(api) { img.style.setProperty("background-size", "cover"); } } - }), + }); + }, { onlyStream: true, id: "discourse-lazy-load-after-adopt",