Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy-images does not work with srcset #22

Open
BlindDespair opened this issue Nov 29, 2020 · 3 comments
Open

lazy-images does not work with srcset #22

BlindDespair opened this issue Nov 29, 2020 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@BlindDespair
Copy link

Thank you for creating this plugin. Although unfortunately when I tried it out it didn't work for me. src were not replaced with data-src and all the images were loaded instantly. But it did produce a lot of layout shifting due to image size changing. All I did was just included the plugin in defaultPostrenderes and that's all. But even if the sizing issue is gone, I checked the source code and it's lacking handling of srcset which I have quite a lot on my site.
I could help addressing these issues, but I need some guidance.

@marcjulian marcjulian added enhancement New feature or request help wanted Extra attention is needed labels Nov 30, 2020
@marcjulian
Copy link
Member

@BlindDespair yes the content will shift when the images are loaded lazily. I think the following line is only getting the attribute src and not srcset from an img element. You could check if either src or srcset is available as attribute to handle both scenarios.

const src = imgEl[i].getAttribute('src');

@marcjulian
Copy link
Member

marcjulian commented Nov 30, 2020

Actually the content should not resize when the images are loaded lazy. An empty svg with the size of the image is added as a placeholder to the src attribute of the img element, thus the content around the images is already in place and the placeholder svg is replaced by the actual image if scrolled in to viewport.

imgEl[i].setAttribute(
'src',
`data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ${dimensions.width} ${dimensions.height}'%3E%3C/svg%3E`
);

@BlindDespair
Copy link
Author

@marcjulian thank you for the response. Actually I figured what was screwing me up was Angular, when it was bootrapping on top of the prerendered page, it simply threw out the img tags with data-src attributes and recreated them again with srcs. That's why all the images were still loaded after startup instead of lazy load. So I instead wrote a really simple angular-based solution with a directive and a service that do the lazy loading. Unfortunately, a scully plugin was not good for me in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants