Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit 1f22035

Browse files
authored
Merge pull request #723 from Shopify/improve-lazyloading-rule
Improve rule for lazy loading to prevent developers from overusing it
2 parents 97c8dce + dec507c commit 1f22035

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

docs/checks/img_lazy_loading.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ Very often, webpages contain many images that contribute to data-usage and how f
88

99
_Quoted from [MDN - Lazy loading][mdn]_
1010

11+
As a general rule you should apply `loading="lazy"` to elements that are **not initially visible** when the page loads. Only images that require user interaction (scrolling, hovering, clicking etc.) to be seen can be safely lazy loaded without negatively impacting the rendering performance.
12+
1113
## Check Details
1214

13-
This check is aimed at defering loading non-critical images.
15+
This check is aimed at deferring loading non-critical images.
1416

1517
:-1: Examples of **incorrect** code for this check:
1618

1719
```liquid
1820
<img src="a.jpg">
1921
20-
<!-- Replaces lazysize.js -->
21-
<img src="a.jpg" class="lazyload">
22+
<!-- Replaces lazysizes.js -->
23+
<img data-src="a.jpg" class="lazyload">
2224
2325
<!-- `auto` is deprecated -->
2426
<img src="a.jpg" loading="auto">
@@ -29,7 +31,7 @@ This check is aimed at defering loading non-critical images.
2931
```liquid
3032
<img src="a.jpg" loading="lazy">
3133
32-
<!-- `eager` is also accepted, but prefer `lazy` -->
34+
<!-- `eager` is also accepted -->
3335
<img src="a.jpg" loading="eager">
3436
```
3537

@@ -44,7 +46,7 @@ ImgLazyLoading:
4446
4547
## When Not To Use It
4648
47-
If you don't want to defer loading of images, then it's safe to disable this rule.
49+
There should be no cases where disabling this rule is needed. When it comes to rendering performance, it is generally better to specify `loading="eager"` as a default. You may want to do that for sections that are often placed in different parts of the page (top, middle, bottom), which makes it hard to reason about which value should be used.
4850

4951
## Version
5052

lib/theme_check/checks/img_lazy_loading.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ class ImgLazyLoading < HtmlCheck
1010
def on_img(node)
1111
loading = node.attributes["loading"]&.downcase
1212
return if ACCEPTED_LOADING_VALUES.include?(loading)
13-
if loading == "auto"
14-
add_offense("Prefer loading=\"lazy\" to defer loading of images", node: node)
15-
else
16-
add_offense("Add a loading=\"lazy\" attribute to defer loading of images", node: node)
17-
end
13+
add_offense("Use loading=\"eager\" for images visible in the viewport on load and loading=\"lazy\" for others", node: node)
1814
end
1915
end
2016
end

test/checks/img_lazy_loading_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_reports_missing_loading_lazy_attribute
2424
END
2525
)
2626
assert_offenses(<<~END, offenses)
27-
Add a loading="lazy" attribute to defer loading of images at templates/index.liquid:1
27+
Use loading="eager" for images visible in the viewport on load and loading="lazy" for others at templates/index.liquid:1
2828
END
2929
end
3030

@@ -36,7 +36,7 @@ def test_prefer_lazy_to_auto
3636
END
3737
)
3838
assert_offenses(<<~END, offenses)
39-
Prefer loading="lazy" to defer loading of images at templates/index.liquid:1
39+
Use loading="eager" for images visible in the viewport on load and loading="lazy" for others at templates/index.liquid:1
4040
END
4141
end
4242
end

0 commit comments

Comments
 (0)