Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR creates a new class method in
URLBuilder
that will generate asrcset
string, which can be used in thesrcset
attribute on an<img>
HTML element.createSrcSet()
takes apath
andparams
argument similar tocreateURL()
, and will return aString
in one of twosrcset
formats.Srcset Width-Pairs
The first format creates
srcset
width-pairs, a comma-delimited list of URLs and width descriptors corresponding to each one. This allows for responsive size switching based on the current width of the browser viewport.createSrcSet()
will append anyparams
passed to the method to each URL constructed within the list. The following is an example of how to generate a width-pairsrcset
:will return the following
String
(collapsed for brevity):Notice that a
w=
parameter is automatically inserted for each entry in thesrcset
list. This is required in order for the element to properly display the correctly-sized image corresponding to the viewport's width.Device Pixel Ratio (DPR) Srcset
The second format this method can return is a
srcset
list of same-size images in varying resolutions. In this case, images are scaled using thedpr=
parameter to adjust for the device pixel ratio of the browser. A DPRsrcset
will be automatically generated instead of a width-pair srcset if exact dimensions for the output image are specified, by providing either aw
(width) or ah
(height) andar
(aspect ratio) in theparams
argument. The following is an example of how to generate a DPRsrcset
:which will return the following
String
:Signing URLs
createSrcSet()
also supports signing URLs, which can be very useful for generating server-side and then passing it to the client. This will avoid having to expose your imgix source's secure token to the client.generates a
srcset
with unique signatures for each URL:For more information on
srcset
, building responsive images, and resolution switching: