-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat: add ability to configure the srcset width tolerance #54
Conversation
lib/imgix/path.rb
Outdated
@@ -131,7 +131,10 @@ def has_query? | |||
def build_srcset_pairs(params) | |||
srcset = '' | |||
|
|||
for width in @target_widths do | |||
width_tolerance = params.delete('width_tolerance'.to_sym) | |||
widths = width_tolerance == DEFAULT_WIDTH_TOLERANCE ? @target_widths : TARGET_WIDTHS.call(width_tolerance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jayeb The sort of gotcha to all this is that we already calculate the default TARGET_WIDTHS
when we instantiate a new Path
object. But if the user opts for a different width tolerance, we end up recalculating a new set of widths on the fly. This is obviously going to cause us to perform repeated work whenever a user wants to specify their own tolerance, but in the grand scheme of things I still don't want to be calculating the widths every time we call to_srcset
I'm toying around with the idea of storing the custom widths locally to avoid recalculating on subsequent calls to to_srcset(width_tolerance:x)
, but might just hold off until all other srcset features are built out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code all looks good. I'm not sure I agree with the decision to define this value as an integer, though. I would suggest we request the value as a unit scalar instead, so it better represents a percentage. This way we'd cut down on potential confusion where a user might think that the integer value they've provided represents an absolute value by which to scale widths, rather than a multiplier.
Co-Authored-By: Jason Eberle <[email protected]>
405c776
to
d83e78d
Compare
…ately from the other parameters
d83e78d
to
64395e4
Compare
This PR adds logic which will allow users to modify the width tolerance used when building
srcset
width pairs. More specifically, the width tolerance dictates the maximum allowable percent difference between entries in asrcset
attribute. By default, this rate is set to 8 percent. Users can use this setting to fine tune how manysrcset
pairs they generate when callingImgix::Path#to_srcset
.A user can now specify their own tolerance rate by passing a percent value to the keyword parameter
width_tolerance
. See the following example:In this case, the
width_tolerance
is set to 20 percent, which will be reflected in the difference between subsequent widths in asrcset
pair: