Skip to content

Instantly share code, notes, and snippets.

@joebui
Last active November 26, 2019 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebui/2dd26ef27b4185ab912e7da4a4bb4280 to your computer and use it in GitHub Desktop.
Save joebui/2dd26ef27b4185ab912e7da4a4bb4280 to your computer and use it in GitHub Desktop.
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