feat(remark-images): Added support for WebP versions in addition to fallbacks. #6495
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.
Hey there,
This PR adds an option to
gatsby-remark-images
to allow for adding webp images alongside the regular formats as fallbacks.Usage
If a previous config was:
Adding the following will generate WebP files alongside the fallbacks.
Additionally, you can pass an options object in order to have different options for the WebP files, such as a higher quality (for the same file size cost).
Implementation Details
gatsby-plugin-sharp
has an additional key returned from thefluid
function:srcSetType
. It returns essentiallyimage/jpeg
orimage/webp
, etc, depending on what the end mimetype is. It has support for png, jpeg, webp - the ones in the documentation. I'm not sure if tiff is supported by these other plugins? It's not in the documentation as an option so I didn't add it here.gatsby-remark-images
checks for the presence of thewithWebp
option. If it's not set, it renders the image as usual:If it is set, it will render out WebP versions with either the options previously provided for the fallbacks, or custom options if they are set.
The html will then resemble this:
This has the advantage of not requiring javascript in order for the optimum image set to be used. Old browsers use the
img
tag as usual, modern browsers will utilise the<source/>
srcsets. Chrome and other WebP supporting browsers will utilise the WebP version of the images.I also added minor doc changes to
gatsby-remark-images
, there was a missing period in a couple lines. I've also added explicit notation that the quality option is available since I had trouble finding out it existed when I started this journey. ThewithWebp
option usage has also been added to the docs.