[
  {
    "path": ".htaccess",
    "content": "<IfModule mod_rewrite.c>\n  RewriteEngine On\n\n  # Check if browser supports WebP images\n  RewriteCond %{HTTP_ACCEPT} image/webp\n\n  # Check if WebP replacement image exists\n  RewriteCond %{DOCUMENT_ROOT}/$1.webp -f\n\n  # Serve WebP image instead\n  RewriteRule (.+)\\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]\n</IfModule>\n\n<IfModule mod_headers.c>\n  # Vary: Accept for all the requests to jpeg, png and gif\n  Header append Vary Accept env=REQUEST_image\n</IfModule>\n\n<IfModule mod_mime.c>\n  AddType image/webp .webp\n</IfModule>\n"
  },
  {
    "path": "README.md",
    "content": "# WebP images with htaccess\nThis snippet detects if the browser [supports WebP](http://caniuse.com/#search=webp) images and then serves a .webp image instead of jpg/png if a .webp file is available at the same location as the supplied jpg/png/gif. Read more about the webp format and other ways to serve it here: [https://images.guide](https://images.guide/#how-do-i-serve-webp).\n\n## Usage\nPlace the following in your .htaccess file and jpg/png/gif images will be replaced with WebP images if found in the same folder.\n```apache\n<IfModule mod_rewrite.c>\n  RewriteEngine On\n\n  # Check if browser supports WebP images\n  RewriteCond %{HTTP_ACCEPT} image/webp\n\n  # Check if WebP replacement image exists\n  RewriteCond %{DOCUMENT_ROOT}/$1.webp -f\n\n  # Serve WebP image instead\n  RewriteRule (.+)\\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]\n</IfModule>\n\n<IfModule mod_headers.c>\n  # Vary: Accept for all the requests to jpeg, png and gif\n  Header append Vary Accept env=REQUEST_image\n</IfModule>\n\n<IfModule mod_mime.c>\n  AddType image/webp .webp\n</IfModule>\n```\n\n## Preferred solution\nControlling your files using htaccess sure is fun, but a more responsible way is to use the `<picture>`-element instead of this solution. It has [great support](https://caniuse.com/webp) in all major browsers and has a built-in fallback for those without it.\n```html\n<picture>\n  <source srcset=\"/path/to/image.webp\" type=\"image/webp\">\n  <img src=\"/path/to/image.jpg\" alt=\"\">\n</picture>\n```\n"
  },
  {
    "path": "index.html",
    "content": "<!doctype html>\n<html>\n  <head>\n    <meta charset=\"utf-8\">\n    <title>WebP image replacement with htaccess</title>\n    <style>\n      * {\n        margin: 0\n      }\n\n      body {\n        padding: 20px;\n      }\n\n      div {\n        display: inline-block;\n        max-width: 350px;\n        margin: 20px 20px 0 0;\n      }\n\n      img {\n        width: 100%;\n      }\n\n      span {\n        display: block;\n        width: 100%;\n        height: 50vh;\n        background: 0 100% / cover transparent no-repeat url('/images/image1.jpg');\n      }\n    </style>\n  </head>\n  <body>\n    <h1>WebP image replacement with htaccess</h1>\n    <p>Browser support WebP: <strong class=\"js-test\">loading...</strong></p>\n\n    <main>\n      <div>\n        <p>1. This image is available as both webp and jpg.</p>\n        <img src=\"images/image1.jpg\" />\n      </div>\n      <div>\n        <p>2. This image is only available as a jpg.</p>\n        <img src=\"images/image2.jpg\" />\n      </div>\n      <div>\n        <p>3. This div has an image background set with a jpg image. It should serve a webp image if supported.</p>\n        <span class=\"background-div\"></span>\n      </div>\n    </main>\n\n    <script src=\"modernizr-webp.js\"></script>\n    <script>\n      Modernizr.on('webp', function (result) {\n        document.querySelector('.js-test').textContent = result\n      })\n    </script>\n  </body>\n</html>\n"
  },
  {
    "path": "modernizr-webp.js",
    "content": "/*! modernizr 3.6.0 (Custom Build) | MIT *\n * https://modernizr.com/download/?-webp-setclasses !*/\n !function(e,n,A){function o(e,n){return typeof e===n}function t(){var e,n,A,t,a,i,l;for(var f in r)if(r.hasOwnProperty(f)){if(e=[],n=r[f],n.name&&(e.push(n.name.toLowerCase()),n.options&&n.options.aliases&&n.options.aliases.length))for(A=0;A<n.options.aliases.length;A++)e.push(n.options.aliases[A].toLowerCase());for(t=o(n.fn,\"function\")?n.fn():n.fn,a=0;a<e.length;a++)i=e[a],l=i.split(\".\"),1===l.length?Modernizr[l[0]]=t:(!Modernizr[l[0]]||Modernizr[l[0]]instanceof Boolean||(Modernizr[l[0]]=new Boolean(Modernizr[l[0]])),Modernizr[l[0]][l[1]]=t),s.push((t?\"\":\"no-\")+l.join(\"-\"))}}function a(e){var n=u.className,A=Modernizr._config.classPrefix||\"\";if(c&&(n=n.baseVal),Modernizr._config.enableJSClass){var o=new RegExp(\"(^|\\\\s)\"+A+\"no-js(\\\\s|$)\");n=n.replace(o,\"$1\"+A+\"js$2\")}Modernizr._config.enableClasses&&(n+=\" \"+A+e.join(\" \"+A),c?u.className.baseVal=n:u.className=n)}function i(e,n){if(\"object\"==typeof e)for(var A in e)f(e,A)&&i(A,e[A]);else{e=e.toLowerCase();var o=e.split(\".\"),t=Modernizr[o[0]];if(2==o.length&&(t=t[o[1]]),\"undefined\"!=typeof t)return Modernizr;n=\"function\"==typeof n?n():n,1==o.length?Modernizr[o[0]]=n:(!Modernizr[o[0]]||Modernizr[o[0]]instanceof Boolean||(Modernizr[o[0]]=new Boolean(Modernizr[o[0]])),Modernizr[o[0]][o[1]]=n),a([(n&&0!=n?\"\":\"no-\")+o.join(\"-\")]),Modernizr._trigger(e,n)}return Modernizr}var s=[],r=[],l={_version:\"3.6.0\",_config:{classPrefix:\"\",enableClasses:!0,enableJSClass:!0,usePrefixes:!0},_q:[],on:function(e,n){var A=this;setTimeout(function(){n(A[e])},0)},addTest:function(e,n,A){r.push({name:e,fn:n,options:A})},addAsyncTest:function(e){r.push({name:null,fn:e})}},Modernizr=function(){};Modernizr.prototype=l,Modernizr=new Modernizr;var f,u=n.documentElement,c=\"svg\"===u.nodeName.toLowerCase();!function(){var e={}.hasOwnProperty;f=o(e,\"undefined\")||o(e.call,\"undefined\")?function(e,n){return n in e&&o(e.constructor.prototype[n],\"undefined\")}:function(n,A){return e.call(n,A)}}(),l._l={},l.on=function(e,n){this._l[e]||(this._l[e]=[]),this._l[e].push(n),Modernizr.hasOwnProperty(e)&&setTimeout(function(){Modernizr._trigger(e,Modernizr[e])},0)},l._trigger=function(e,n){if(this._l[e]){var A=this._l[e];setTimeout(function(){var e,o;for(e=0;e<A.length;e++)(o=A[e])(n)},0),delete this._l[e]}},Modernizr._q.push(function(){l.addTest=i}),Modernizr.addAsyncTest(function(){function e(e,n,A){function o(n){var o=n&&\"load\"===n.type?1==t.width:!1,a=\"webp\"===e;i(e,a&&o?new Boolean(o):o),A&&A(n)}var t=new Image;t.onerror=o,t.onload=o,t.src=n}var n=[{uri:\"data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=\",name:\"webp\"},{uri:\"data:image/webp;base64,UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAABBxAR/Q9ERP8DAABWUDggGAAAADABAJ0BKgEAAQADADQlpAADcAD++/1QAA==\",name:\"webp.alpha\"},{uri:\"data:image/webp;base64,UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA\",name:\"webp.animation\"},{uri:\"data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=\",name:\"webp.lossless\"}],A=n.shift();e(A.name,A.uri,function(A){if(A&&\"load\"===A.type)for(var o=0;o<n.length;o++)e(n[o].name,n[o].uri)})}),t(),a(s),delete l.addTest,delete l.addAsyncTest;for(var p=0;p<Modernizr._q.length;p++)Modernizr._q[p]();e.Modernizr=Modernizr}(window,document);\n"
  }
]