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

Add support for lazysizes.js #429

Closed
boxedpaul opened this issue Nov 24, 2020 · 1 comment
Closed

Add support for lazysizes.js #429

boxedpaul opened this issue Nov 24, 2020 · 1 comment
Assignees

Comments

@boxedpaul
Copy link

Feature request for Cloudinary Ruby SDK

Please add a 'switch' for enabling lazysizes loading. This requires the tag to use the "data-src" attribute instead of the "src" attribute on the image.

Use case

I have a site with a lot of images that Google doesn't like (from an SEO perspective) because I'm loading all the images on initial page render despite optimising with Cloudinary to reduce file size.

Lazy loading images means you load them only when the user scrolls the page such that the image is visible. This can save significantly on download time when rendering the page. This is important for SEO.

Lazysizes.js is a very popular script for doing this, but it relies on swapping the "src" attribute for "data-src".

Do you have a proposed solution?

In the cl_image_tag helper, provide an option to swap the "src" attribute for "data-src". E.g.

link_to cl_image_tag(resource.image, lazysizes: true)

Result:

<img data-src="/resource_image.jpg" class="lazyload">

You could make this more extensible by making this configurable:

link_to cl_image_tag(resource.image, src: 'data-src')

Thanks.

@boxedpaul
Copy link
Author

boxedpaul commented Nov 24, 2020

Okay...so as soon as I posted this I realised I could do the following:

link_to cl_image_tag(resource.image).gsub(' src="', ' data-src="').html_safe

Works like a charm, and maybe worth mentioning for anyone else that gets stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants