Skip to content

Commit e3b0163

Browse files
committed
refactor(core): optimize image loading
- JavaScript runs so fast that LQIP will never be detected - Increase the running priority of image processing in posts Enhancement for #1267
1 parent b489da8 commit e3b0163

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

_includes/refactor-content.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
8282
{% assign _attrs = _left | split: '" ' %}
8383

84+
{% assign _src = null %}
8485
{% assign _lqip = null %}
8586
{% assign _class = null %}
8687

@@ -119,8 +120,9 @@
119120
{% endunless %}
120121

121122
{% if _lqip %}
122-
{% if _lqip contains ':' %}
123+
{% if _lqip contains 'data:' %}
123124
{% assign _lazyload = false %}
125+
{% assign _class = _class | append: ' blur' %}
124126
{% else %}
125127
{% assign _lqip_alt = 'lqip="' | append: _path_prefix %}
126128
{% assign _left = _left | replace: 'lqip="', _lqip_alt %}

_javascript/modules/components/img-loading.js

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,60 @@
22
* Setting up image lazy loading and LQIP switching
33
*/
44

5-
export function loadImg() {
6-
const $images = $('main img[loading="lazy"]');
7-
const $lqip = $('main img[data-lqip="true"]');
5+
const ATTR_DATA_SRC = 'data-src';
6+
const ATTR_DATA_LQIP = 'data-lqip';
7+
const C_SHIMMER = 'shimmer';
8+
const C_BLUR = 'blur';
89

9-
if ($images.length > 0) {
10-
$images.on('load', function () {
11-
/* Stop shimmer when image loaded */
12-
$(this).parent().removeClass('shimmer');
13-
});
10+
function handleImage() {
11+
const $img = $(this);
1412

15-
$images.each(function () {
16-
/* Images loaded from the browser cache do not trigger the 'load' event */
17-
if ($(this).prop('complete')) {
18-
$(this).parent().removeClass('shimmer');
19-
}
20-
});
13+
if (this.hasAttribute(ATTR_DATA_LQIP) && this.complete) {
14+
$img.parent().removeClass(C_BLUR);
15+
} else {
16+
$img.parent().removeClass(C_SHIMMER);
17+
}
18+
}
19+
20+
/* Switch LQIP with real image url */
21+
function switchLQIP(img) {
22+
// Sometimes loaded from cache without 'data-src'
23+
if (img.hasAttribute(ATTR_DATA_SRC)) {
24+
const $img = $(img);
25+
const dataSrc = $img.attr(ATTR_DATA_SRC);
26+
$img.attr('src', encodeURI(dataSrc));
27+
}
28+
}
29+
30+
export function loadImg() {
31+
const $images = $('article img');
32+
33+
if ($images.length) {
34+
$images.on('load', handleImage);
2135
}
2236

23-
if ($lqip.length > 0) {
24-
$lqip.each(function () {
25-
/* Switch LQIP with real image url */
26-
const dataSrc = $(this).attr('data-src');
27-
$(this).attr('src', encodeURI(dataSrc));
28-
$(this).removeAttr('data-src data-lqip');
37+
/* Images loaded from the browser cache do not trigger the 'load' event */
38+
$('article img[loading="lazy"]').each(function () {
39+
if (this.complete) {
40+
$(this).parent().removeClass(C_SHIMMER);
41+
}
42+
});
43+
44+
const $lqips = $(`article img[${ATTR_DATA_LQIP}="true"]`);
45+
46+
if ($lqips.length) {
47+
const isHome = $('#post-list').length > 0;
48+
49+
$lqips.each(function () {
50+
if (isHome) {
51+
// JavaScript runs so fast that LQIPs in home page will never be detected
52+
// Delay 50ms to ensure LQIPs visibility
53+
setTimeout(() => {
54+
switchLQIP(this);
55+
}, 50);
56+
} else {
57+
switchLQIP(this);
58+
}
2959
});
3060
}
3161
}

_javascript/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
toc
88
} from './modules/plugins';
99

10-
basic();
1110
initSidebar();
1211
initTopbar();
1312
loadImg();
1413
imgPopup();
1514
initLocaleDatetime();
1615
initClipboard();
1716
toc();
17+
basic();

_sass/addon/commons.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ a {
7171
img {
7272
max-width: 100%;
7373
height: auto;
74-
transition: all 0.7s ease-in-out;
74+
transition: all 0.35s ease-in-out;
7575

76-
&[data-lqip='true'] {
76+
.blur & {
7777
$blur: 20px;
7878

7979
-webkit-filter: blur($blur);

0 commit comments

Comments
 (0)