Skip to content

Instantly share code, notes, and snippets.

@branneman
Created May 5, 2017 12:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branneman/4a334a14434a5d9f2b57ae59dc85b875 to your computer and use it in GitHub Desktop.
Save branneman/4a334a14434a5d9f2b57ae59dc85b875 to your computer and use it in GitHub Desktop.
AWS Lambda@Edge whitelisted image to webp request mapper
'use strict';
const whitelist = {
'/static/img/photo.jpg': '/static/img/photo.webp'
};
exports.handler = (event, context, callback) => {
// Grab HTTP request and it's headers
const request = event.Records[0].cf.request;
const headers = request.headers;
// Detect WebP support and see if the file is whitelisted
const supportsWebp = /.*(image\/webp).*/.test(headers.Accept);
const isWhitelisted = !!whitelist[request.uri];
if (supportsWebp && isWhitelisted) {
request.uri = whitelist[request.uri];
}
return callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment