mirror of
https://github.com/hanzoai/forum.git
synced 2026-09-30 21:51:19 +00:00
Lazy loading images naturally causes a slight delay, because the browser only starts to load them after laying out the DOM and checking whether they're in the viewport. Plus, in Safari, re-rendering the DOM of a lazy-loaded image always causes a brief flicker, even if the image is already cached in the browser. Lazy-loading is most beneficial on large one-off images which are often rendered outside the viewport. That's frequently the case for images which users share in topics. Avatars, on the other hand, are very small images, they're very often above-the-fold, and the same avatar often occurs many times on the same page. Therefore, this commit removes `loading="lazy"` from avatars, which should improve avatar load times in all browsers, and stop the flicker in Safari. --- Tapping logo to reload topic-list in Safari. Before: https://github.com/user-attachments/assets/242299f8-aa13-4991-b321-2f143603ed26 After: https://github.com/user-attachments/assets/5e5bfd28-3a78-40fd-af21-3d92e7b3ba8a
94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
import { setupTest } from "ember-qunit";
|
|
import { module, test } from "qunit";
|
|
import {
|
|
avatarImg,
|
|
avatarUrl,
|
|
getRawAvatarSize,
|
|
} from "discourse/lib/avatar-utils";
|
|
import { setupURL } from "discourse/lib/get-url";
|
|
|
|
module("Unit | Utilities", function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
test("getRawAvatarSize avoids redirects", function (assert) {
|
|
assert.strictEqual(
|
|
getRawAvatarSize(1),
|
|
24,
|
|
"returns the first size larger on the menu"
|
|
);
|
|
|
|
assert.strictEqual(getRawAvatarSize(2000), 288, "caps at highest");
|
|
});
|
|
|
|
test("avatarUrl", function (assert) {
|
|
assert.blank(avatarUrl("", "tiny"), "no template returns blank");
|
|
assert.strictEqual(
|
|
avatarUrl("/fake/template/{size}.png", "tiny"),
|
|
"/fake/template/" + getRawAvatarSize(24) + ".png",
|
|
"simple avatar url"
|
|
);
|
|
assert.strictEqual(
|
|
avatarUrl("/fake/template/{size}.png", "large"),
|
|
"/fake/template/" + getRawAvatarSize(48) + ".png",
|
|
"different size"
|
|
);
|
|
|
|
setupURL("https://app-cdn.example.com", "https://example.com", "");
|
|
|
|
assert.strictEqual(
|
|
avatarUrl("/fake/template/{size}.png", "large"),
|
|
"https://app-cdn.example.com/fake/template/" +
|
|
getRawAvatarSize(48) +
|
|
".png",
|
|
"uses CDN if present"
|
|
);
|
|
});
|
|
|
|
let setDevicePixelRatio = function (value) {
|
|
if (Object.defineProperty && !window.hasOwnProperty("devicePixelRatio")) {
|
|
Object.defineProperty(window, "devicePixelRatio", { value: 2 });
|
|
} else {
|
|
window.devicePixelRatio = value;
|
|
}
|
|
};
|
|
|
|
test("avatarImg", function (assert) {
|
|
let oldRatio = window.devicePixelRatio;
|
|
setDevicePixelRatio(2);
|
|
|
|
let avatarTemplate = "/path/to/avatar/{size}.png";
|
|
assert.strictEqual(
|
|
avatarImg({ avatarTemplate, size: "tiny" }),
|
|
"<img alt='' width='24' height='24' src='/path/to/avatar/48.png' class='avatar'>",
|
|
"it returns the avatar html"
|
|
);
|
|
|
|
assert.strictEqual(
|
|
avatarImg({
|
|
avatarTemplate,
|
|
size: "tiny",
|
|
title: "evilest trout",
|
|
}),
|
|
"<img alt='' width='24' height='24' src='/path/to/avatar/48.png' class='avatar' title='evilest trout'>",
|
|
"it adds a title if supplied"
|
|
);
|
|
|
|
assert.strictEqual(
|
|
avatarImg({
|
|
avatarTemplate,
|
|
size: "tiny",
|
|
extraClasses: "evil fish",
|
|
}),
|
|
"<img alt='' width='24' height='24' src='/path/to/avatar/48.png' class='avatar evil fish'>",
|
|
"it adds extra classes if supplied"
|
|
);
|
|
|
|
assert.blank(
|
|
avatarImg({ avatarTemplate: "", size: "tiny" }),
|
|
"it doesn't render avatars for invalid avatar template"
|
|
);
|
|
|
|
setDevicePixelRatio(oldRatio);
|
|
});
|
|
});
|