Does WebP work with Varnish?
WebP images are compatible with the Varnish cache. The compatibility was introduced to the module in version 2.1.2. Make sure that you have the latest version of the extension installed.
To make WebP images load on the website, it is required to adjust the Varnish configuration. Please contact your hosting provider on this matter or follow the steps below.
Step 1. Open the Varnish configuration file. Usually, it is placed here:
/usr/varnish/default.vcl
Step 2. Find the vcl_hash subroutine and paste this code to it:
# Amasty Google Page Speed Optimizer device detection and cache separation mechanism
if (req.http.cookie !~ "X-Magento-Vary=") {
call device_detect;
if ((req.http.X-Amasty-Accept-Avif || req.http.X-Amasty-Accept-Webp) && req.http.X-Amasty-Device) {
hash_data(req.http.X-Amasty-Accept-Avif);
hash_data(req.http.X-Amasty-Accept-Webp);
hash_data(req.http.X-Amasty-Device);
}
}
Step 3. Add a new subroutine using this code:
sub device_detect {
unset req.http.X-Amasty-Accept-Webp;
unset req.http.X-Amasty-Accept-Avif;
unset req.http.X-Amasty-Device;
if (req.http.Accept ~ "image\/webp" ||
req.http.User-Agent ~ "(?i)(\biPhone|\biPod|\biPad|\bMacintosh).*Version\/(1[4-9]|[2-9][0-9])" ||
req.http.User-Agent ~ "(?i)Edg|Firefox|Chrome|Opera"
) {
set req.http.X-Amasty-Accept-Webp = "true";
}
if (req.http.Accept ~ "image\/avif" ||
req.http.User-Agent ~ "(?i)(?!.*Edg)(Firefox|Chrome|Opera)" ||
req.http.User-Agent ~ "(?i)(\bMacintosh).*Version\/(1[6-9].[4-9]|1[7-9]|[2-9][0-9])" ||
req.http.User-Agent ~ "(?i)(\biPhone|\biPod|\biPad).*Version\/(1[6-9]|[2-9][0-9])"
) {
set req.http.X-Amasty-Accept-Avif = "true";
}
if (req.http.User-Agent ~ "(?i)Mobile") {
set req.http.X-Amasty-Device = "mobile";
} elsif (req.http.User-Agent ~ "(?i)Tablet") {
set req.http.X-Amasty-Device = "tablet";
} else {
set req.http.X-Amasty-Device = "desktop";
}
}
Step 4. Now restart Varnish and enjoy WebP images on your website.
Related Articles
How to optimize images using Google Page Speed Optimizer extension?
In order to optimize images using Google Page Speed Optimizer extension, first, you need to create WebP copies and make sure they are displayed on the frontend. Please follow the steps below to create WebP copies of the images. Step 1. In Amasty → ...
What PageSpeed Insights metrics does Google Page Speed Optimizer cover?
The Google Page Speed Optimizer extension is designed to optimize images and code, which can help to improve certain PageSpeed Insights metrics and enhance the overall website performance. Our extension guarantees up to 97% rating if the fast server ...
Why are Webp images not appearing on the frontend?
There are several reasons why WebP images might not be pulled correctly on your website. 1. As the first step, make sure that WebP copies of the images were created. To check this, you can open any storefront image in a new tab and modify its URL the ...
Does your extension work with Magento Cloud?
Our Google Page Speed Optimizer extension is fully compatible with Magento CE and EE edition, including Cloud instances. The only thing is that the image optimization functionality will be missing. The extension uses the 3rd party tools for ...
What optimizer settings do I use for images, if I have both the Jet Theme and the Google Page Speed Optimizer module?
The Image Optimizer inside Jet Theme is a Lite version of any optimizer-type modules. So, considering you have the Google Page Speed Optimizer extension, it leads over the optimizer settings inside the Jet Theme. If you have already installed, or are ...