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 lazy image loading #108

Merged
merged 4 commits into from
Dec 3, 2020
Merged

Conversation

lunks
Copy link
Contributor

@lunks lunks commented Nov 4, 2020

Description

Adds lazy support to ix_image_tag as described on #60. We need this in our project due to the amount of images being loaded on a single webpage, often from very bad network scenarios.

Checklist: New Feature

  • Each commit follows the Conventional Commit format: e.g. chore(readme): fixed typo. See the end of this file for more information.
  • Any breaking changes are specified on the commit on which they are introduced with BREAKING CHANGE in the body of the commit.
  • If this is a big feature with breaking changes, consider opening an issue to discuss first. This is completely up to you, but please keep in mind that your PR might not be accepted.
  • Run unit tests to ensure all existing tests are still passing
  • Add new passing unit tests to cover the code introduced by your PR
  • Update the readme
  • Add some steps so we can test your cool new feature!

Steps to Test

Remap any img attributes needed for the lazy-loading solution of your choice via attribute_options on an ix_image_tag:

<%= ix_image_tag('image.jpg',
	attribute_options: {
		src: "data-src",
		srcset: "data-srcset",
		sizes: "data-sizes"
	},
	url_params: { w: 1000 },
	tag_options: { src: "lqip.jpg" }) %>

Related issue: #60

@frederickfogerty frederickfogerty removed the request for review from a team November 10, 2020 14:40
@sherwinski sherwinski self-requested a review November 10, 2020 21:29
Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lunks, thanks for the PR and apologies for the delayed response here.

Initial thoughts: I think it would be worth adding extra documentation to guide users on how to integrate lazysizes into their app -- even if that just includes a link to an external guide. Since without it enabled, this feature would not work correctly.

@lunks
Copy link
Contributor Author

lunks commented Nov 10, 2020

Hey @lunks, thanks for the PR and apologies for the delayed response here.

Initial thoughts: I think it would be worth adding extra documentation to guide users on how to integrate lazysizes into their app -- even if that just includes a link to an external guide. Since without it enabled, this feature would not work correctly.

Trying to follow what your peers did at react-imgix: https://github.com/imgix/react-imgix#lazy-loading is it ok to add a section to README similarly? An external guide could make sense, but I think it's out of the scope of the library or the PR. Most people as you can see on #60 already found a lazy-loading library suit to their causes, they just need imgix-rails to support the standard.

@sherwinski
Copy link
Contributor

Yes, I think an added section to the README (with example) would suffice 👍

@lunks
Copy link
Contributor Author

lunks commented Nov 10, 2020

Done @sherwinski, can you take a look?

Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding that @lunks.
Just a couple of small nitpicks to change. After that, I'd like to finish testing the feature out myself before merging.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Co-authored-by: sherwinski <sherwinhb@gmail.com>
@lunks
Copy link
Contributor Author

lunks commented Nov 11, 2020

Thanks for adding that @lunks.
Just a couple of small nitpicks to change. After that, I'd like to finish testing the feature out myself before merging.

Thanks for the suggestions, @sherwinski. It reads much better now.

@lunks
Copy link
Contributor Author

lunks commented Nov 20, 2020

Hey @sherwinski any updates?

Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lunks,

I tested the feature and it works great 👍 I did find having to add lazysizes manually to my app a little awkward so I was curious if you knew of a way to ship this gem with the library baked in? An added benefit of that would be that we can control the version of the dependent library to ensure that it works as expected.

lib/imgix/rails/image_tag.rb Outdated Show resolved Hide resolved
@lunks
Copy link
Contributor Author

lunks commented Nov 20, 2020

Hey @lunks,

I tested the feature and it works great 👍 I did find having to add lazysizes manually to my app a little awkward so I was curious if you knew of a way to ship this gem with the library baked in? An added benefit of that would be that we can control the version of the dependent library to ensure that it works as expected.

Like I mentioned in the other reply, I believe this would be out of the scope of the gem. See react-imgix, it tries to support it but be agnostic enough that you can use other libs that do a similar job.

@sherwinski
Copy link
Contributor

I see your point. In that case, the interface should match how we implemented it in react-imgix, which allows the user to set the attributes themselves. I like the proposed interface here but I think it would make more sense if we were going to ship it with a lazy loading library baked in. Right now, it feels a bit too in between being configurable and a full feature. To make the interface more generalized we should:

  • Replace the lazy field with attribute_options, which allows the user to remap attributes like data-src, data-srcset, etc. See the example of this on react-imgix.
  • Set the LQIP via tag_options[:src]

@lunks
Copy link
Contributor Author

lunks commented Nov 21, 2020

I see your point. In that case, the interface should match how we implemented it in react-imgix, which allows the user to set the attributes themselves. I like the proposed interface here but I think it would make more sense if we were going to ship it with a lazy loading library baked in. Right now, it feels a bit too in between being configurable and a full feature. To make the interface more generalized we should:

  • Replace the lazy field with attribute_options, which allows the user to remap attributes like data-src, data-srcset, etc. See the example of this on react-imgix.
  • Set the LQIP via tag_options[:src]

I disagree as most libraries besides lazysizes use this very same API.

To list a few:
https://github.com/verlok/vanilla-lazyload
https://github.com/ivopetkov/responsively-lazy

I'll go forward with the proposed API but I think it's a worse API. This works for most cases with lazysizes and other similar plugins. People that need this (as stated on #60) would benefit from this as-is.

@lunks
Copy link
Contributor Author

lunks commented Nov 24, 2020

@sherwinski done, let me know if you have any other requests.

Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your patience and working with me on this @lunks. This looks ready to merge now 👍

@sherwinski sherwinski merged commit a84a941 into imgix:main Dec 3, 2020
@lunks
Copy link
Contributor Author

lunks commented Jan 5, 2021

Hey @sherwinski could you please release a new gem version with this change?

@sherwinski
Copy link
Contributor

Of course and sorry about @lunks, that part somehow slipped past me.
It is now released in v4.2.0

@lunks
Copy link
Contributor Author

lunks commented Jan 5, 2021

Of course and sorry about @lunks, that part somehow slipped past me.
It is now released in v4.2.0

Thanks, happy new year for you and your team!

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

Successfully merging this pull request may close these issues.

2 participants