You can use the picture tag:
<picture> <source type="image/webp" srcset="path/to/image.webp"> <source type="image/jp2" srcset="path/to/image.jp2"> <source type="image/jxr" srcset="path/to/image.jxr"> <img src="path/to/image.jpg"> </picture>
But the picture tag is also supported in few places, so js will be needed: https://github.com/scottjehl/picturefill It is enough just to connect everything:
<script async=true src=/path/to/picturefill.js></script> <script async=true src=/path/to/jxr.js></script> <script async=true src=/path/to/jp2.js></script>
But I would not bother and yet YET ignored the recommendations of Google. Maybe in the future this will be possible to return.
UPD from 01/26/2019: an example of webp kickback webp on a web server, for example, Nginx
As a rule, the browser transmits information about supported technologies to the server. For example, Chrome passes such Accept: image/webp, */*;q=0.8 values Accept: image/webp, */*;q=0.8 . Therefore, you can give certain images on the server side.
Nginx example:
location / { # проверка заголовка Accept и наличия версии файла в .webp if ($http_accept ~* "webp") { set $webp_accept "true"; } if (-f $request_filename.webp) { set $webp_local "true"; } # если WebP есть, то передать Vary if ($webp_local = "true") { add_header Vary Accept; } # если клиент поддерживает WebP, то передать файл if ($webp_accept = "true") { rewrite (.*) $1.webp break; } }
Apache example:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_ACCEPT} image/webp RewriteCond %{DOCUMENT_ROOT}/$1.webp -f RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] </IfModule> <IfModule mod_headers.c> Header append Vary Accept env=REDIRECT_accept </IfModule> AddType image/webp .webp
Examples for different web servers can be found here .