Hi,
I was looking for drupal plugin for responsive or plain image lazy loading, but couldn't find anything good that fits my needs. Also, it can be useful for image heavy sites to improve page load performance.
Discovering JS libraries for image lazy load I found this one https://github.com/aFarkas/lazysizes and it looks pretty good.
I'm interested in implementing such kind of responsive image lazy loading module, but doubt if it's worth to develop another separate module which would be dealing with picture element.
So, I wonder if there is an interest for integration lazysizes with picture module?
Any thoughts, suggestions and comments are welcome!
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | picture-lazyload_css-2491101-33.patch | 689 bytes | sgdev |
| #26 | implement_image_lazy_load-2491101-26.patch | 46.55 KB | baysaa |
Comments
Comment #1
ssemashka commentedComment #2
attiks commentedOff course, feel free to upload a patch.
Ideally all javascript is plain javascript ;-)
Comment #3
attiks commentedSee #2365083: Applying lazyloader in Picture, it already has a patch
Comment #4
ssemashka commentedthanks, I'm currently looking into that. I'll keep this issue updated
Comment #5
ssemashka commentedAdded lazySizes.js and "Picture lazyload" checkbox to field formatter and image manage file display tab ("/admin/config/media/file-types/manage/image/file-display").
Needs review.
Adding aspect ratio support is in my roadmap. Aspect ratio helps to preserve space for picture elements that are not loaded yet and prevents layout reflows while image is lazyloading.
Comment #7
ssemashka commentedRebased patch on latest picture module dev version
Comment #8
attiks commentedAre you still planning on adding aspect ratio support?
Comment #9
ssemashka commentedyes, was little stuck during implementation. I realized applying aspect ratio doesn't make sense when there is a fallback image style selected.
So, what I've done:
<source>tag based on image dimensions from srcset.Let me know what do you think.
Comment #10
attiks commentedI had a quick look at the patch, some questions and small remarks. Will test the patch later today
why the change?
should be removed
why did you remove this?
Comment #11
ssemashka commented@attiks, thanks for reviewing. Those changes were made by chance, sorry. Reverting them. Adding updated patch.
Comment #12
loparr commentedHi, I manually applied the this patch. I can see checkboxes on /admin/structure/file-types/manage/image/file-display (lazyload, aspect ratio). However lazyloading is not working. I even tried to load lazysizes.js using context for every page. I get no error on console nor any system error.
Or do I need to apply the first patch and also second after? Thank you.
Comment #13
ssemashka commentedHi loparr,
You need to apply only the patch from comment #11.
Check your picture element, if you have data-srcset attributes on
<source>tags, then the issue is most likely on JS side. Please, drop me the JS error from browser console, your picture element markup and screenshots what you see, I'll try to investigate it.If you don't see data-srcset attribute on
<source>tags with lazyload checkbox checked, it means something is not configured properly.Give me the information I mentioned above, it will give me more clarity what's happening.
Comment #14
loparr commentedHi,
I checked all settings and found another checkbox in image field in /admin/structure/types/manage/content-type/display. Before I checked only here /admin/structure/file-types/manage/image/file-display (lazyload, aspect ratio)
Now the
<source>have data-srcset attribute and picture has class "array lazyloaded"However now I get error: Notice: Array to string conversion in theme_picture() (line 1381 of /sites/all/modules/picture/picture.module).
And no error in console. The line 1381 from module is "$attributes['class'] = !empty($attributes['class']) ? $attributes['class'] . ' lazyload' : 'lazyload';"
So images inside fields are lazyloaded however images inserted inline (wysiwyg and ckeditor + media) are not lazyloaded. The
<source>tag does not have data-srcset attribute. Images inserted inline without media do not lazyload as well. Tha only place where I could find settings for image is /admin/structure/file-types/manage/image/file-display (lazyload, aspect ratio).Thank you.
Comment #15
ssemashka commentedHi loparr,
Thanks for pointing this out, I wasn't much aware of wysiwyg support. Try new patch from comment #15, I added lazyload support for wysiwyg.
Also, here is the list of places where you can find lazyload checkbox:
Comment #16
attiks commentedComment #17
baysaa commentedThank you for the patch @sergesemashko
The patch at #15 worked well when I tested it on my website. The only issue was I had a bit of trouble getting it to run (and it's probably my own fault to begin with) the first time. I suppose this is more of a documentation thing. I'll explain what happened:
When changing the "fallback" to an empty image, my CSS wasn't set to expand img tags inside pictures to be 100% width of the parent. This caused some headscratches as the heigh on images wasn't being set, and the "addAspectRatio" method failed as the img width was less than 36 pixels (1x1 blank gif). The code below is where it fails. (ls.aspectratio.js: line 158):
All I had to do was apply the CSS below to make the ls.aspectratio.js reserve correct space/placeholder for the image:
picture img { width: 100% }Comment #18
ssemashka commented@Baysaa, thanks for trying patch out.
Actually,
picture img { width: 100% }is in/lazysizes/plugins/aspectratio/ls.aspectratio.css. The file is loaded for me and aspectratio works fine.Maybe you have something that prevents contrib css files from being loaded?
Comment #19
baysaa commented@sergesemashko, I notice now that the CSS is not included when 'picture_polyfill_version' variable is set to 'dev'. The CSS is only included when the variable is set to 'min'.
Comment #20
baysaa commentedHere's a rerolled patch from #15 to also include the ls.aspectratio.css when on 'dev' mode of 'picture_polyfill_version' (See comment #19)
Edit: Bad patch. Uploading a reroll again in a few min.
Comment #22
baysaa commentedTrying again.
Comment #23
baysaa commentedTrigger testbot
Comment #24
baysaa commentedDisplaying latest patch only on summary.
Comment #25
baysaa commentedComment #26
baysaa commentedI can't take credit for this patch. So uploading again with correct author. Sorry for the spam (Bit new to the workflow)
Comment #28
jelle_sFixed in latest dev. Thanks for the patch!
Comment #30
titouilleHi,
Just a little question about the lazyaspectratio... ls.aspectratio.css contains the following rule :
With this rule, when aspectratio is used on at least one picture on the page, ls.aspectratio.css is loaded and apply the rule on all pictures on the page, even if it's not needed (for example pictures as thumbnail in the main content are stretched and pixelised). With this behavior, themer is obligated to add lot of rules to correct the width for any pictures that doesn't need 100% width.
Why don't use a more precise rule like this :
To apply the 100% width only to pictures that need it for the aspect-ratio calculation ? If themer want to apply 100% width to any pictures, I think it's his choice. he doesn't must be obligated to do when enabling the picture module, I'm right ??
Thanks in advance for your answer about that.
Comment #31
Greg Varga commentedI agree with titouille #30. adding the .lazyaspectratio class helps fixing this issue.
Comment #32
attiks commentedCan somebody provide a patch?
Comment #33
sgdev commentedPatch attached.
Comment #35
pawandubey commented@ron_s
There is another issue created for it related to image width
https://www.drupal.org/project/picture/issues/2720209
Please follow the same and closing this issue as the patch is already merged.
Comment #36
sgdev commentedI responded to the module maintainer 3 years ago in providing a patch to resolve the issue.
All of a sudden deciding that you want to point to another issue where you created the same patch 40 minutes ago is not appropriate. Resetting to Needs Review.
Comment #37
joaogarin commentedI think we should include the patch here. it fixes the issue
Comment #38
joaogarin commentedComment #39
thomas.frobieterComment #41
lesleyfernandes commentedThanks @ron_s