Created
May 5, 2017 12:26
-
-
Save branneman/4a334a14434a5d9f2b57ae59dc85b875 to your computer and use it in GitHub Desktop.
AWS Lambda@Edge whitelisted image to webp request mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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