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.
This commit is contained in:
David Taylor 2022-09-14 12:42:59 +01:00 committed by GitHub
parent 09a434e2d8
commit 04e433d286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 2 deletions

View File

@ -17,9 +17,19 @@ function isLoaded(img) {
export function nativeLazyLoading(api) { export function nativeLazyLoading(api) {
api.decorateCookedElement( api.decorateCookedElement(
(post) => (post) => {
const siteSettings = api.container.lookup("service:site-settings");
forEachImage(post, (img) => { forEachImage(post, (img) => {
img.loading = "lazy"; 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 (img.dataset.smallUpload) {
if (!isLoaded(img)) { if (!isLoaded(img)) {
if (!img.onload) { if (!img.onload) {
@ -36,7 +46,8 @@ export function nativeLazyLoading(api) {
img.style.setProperty("background-size", "cover"); img.style.setProperty("background-size", "cover");
} }
} }
}), });
},
{ {
onlyStream: true, onlyStream: true,
id: "discourse-lazy-load-after-adopt", id: "discourse-lazy-load-after-adopt",