Skip to content

Instantly share code, notes, and snippets.

@jkrnak

jkrnak/handler.js

Last active Nov 21, 2018
Embed
What would you like to do?
Example Lambda@Edge function to add cache control header to assets
// 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
You can’t perform that action at this time.