Skip to content

Instantly share code, notes, and snippets.

@jeremy-wagner
Last active January 27, 2021 19:10
Show Gist options
  • Save jeremy-wagner/f9d28c85554b3da01311e9d2fd6d61d8 to your computer and use it in GitHub Desktop.
Save jeremy-wagner/f9d28c85554b3da01311e9d2fd6d61d8 to your computer and use it in GitHub Desktop.
AWS Lambda handler to add cache-control handler.
'use strict';
// Lambda function to set cache control public header in case of missing cache control header
exports.handler = (event, context, callback) => {
const { response } = event.Records[0].cf;
const { headers } = response;
const headerCacheControl = 'Cache-Control';
const defaultTimeToLive = 60 * 60 * 24 * 14; // 14 days
if (response.status === '200') {
if (!headers[headerCacheControl.toLowerCase()]) {
headers[headerCacheControl.toLowerCase()] = [{
key: headerCacheControl,
value: `public, max-age=${defaultTimeToLive}`,
}];
}
}
callback(null, response);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment