Problem/Motivation

When the "Prefer native lazy-loading" box is checked, the "loading" attribute is properly used; however, it has come to my attention that if we're on a browser that does not have native lazy loading available (e.g. Firefox < 75/Chrome < 77/IE <= 11, see https://caniuse.com/loading-lazy-attr), there is no feature detection in use to instead use the JavaScript-based approach. Correct me if I'm wrong, but the prefer native option seems to not necessarily prefer native, but actually insists upon it -- breaking lazy loading for browsers that do not support it natively.

Should we not be doing a more accurate feature detection for the current browser in order to determine if the user's browser supports lazy load, then adjust rendering accordingly? Since this is all happening server-side perhaps this is not possible?

Steps to reproduce

  1. Configure a site to use the Lazy-Load module.
  2. Ensure that the "Prefer native lazy-loading" option is checked.
  3. Navigate to the site using a web browser that does not support native lazy-loading (i.e via a service such as Browserstack), and confirm that image are not lazy loaded, because the markup is not adjust from the native lazy-loading approach.

Proposed resolution

Add more accurate feature detection when selecting the prefer native option. If the browser is determined to support native lazy loading, use the loading="lazy" attribute, otherwise adjust the markup to leverage the lazysizes JS.

Comments

afireintheattic created an issue. See original summary.

afireintheattic’s picture

Issue summary: View changes
melinda_ksz’s picture

StatusFileSize
new1.14 KB

Here is a patch that loads lazysizes scripts only if the preferNative option is not selected, or if the browser do not support native lazyloading.
This way, even if we have preferNative option selected, in case the browser doesn't support native lazy loading, the JavaScript-based approach is being used.
We worked this out together with @hchonov

melinda_ksz’s picture

Status: Active » Needs review
hchonov’s picture

Status: Needs review » Reviewed & tested by the community

RTBC since we've already tested in a project.

heni_deepak’s picture

Status: Reviewed & tested by the community » Needs work

#3 patch failed.

alexortega_98’s picture

alexortega_98’s picture

Status: Needs work » Needs review