Last active
November 26, 2019 06:07
-
-
Save joebui/2dd26ef27b4185ab912e7da4a4bb4280 to your computer and use it in GitHub Desktop.
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
exports.handler = (event, context, callback) => { | |
const response = event.Records[0].cf.response; | |
const requestUri = event.Records[0].cf.request.uri; | |
try { | |
if (requestUri.endsWith('.br')) { | |
// the browser to decode using brotli algorithm. | |
addContentEncoding('br', response); | |
} else { | |
if (requestUri.endsWith('.gz')) { | |
// or to decode using gzip algorithm. | |
addContentEncoding('gzip', response); | |
} | |
} | |
} catch (e) { | |
console.log(`Error occurred with with ${requestUri}: ${JSON.stringify(e)}`); | |
} finally { | |
callback(null, response); | |
} | |
}; | |
function addContentEncoding(value, response) { | |
response.headers['content-encoding'] = [ | |
{ | |
key: 'Content-Encoding', | |
value | |
} | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment