Last active
January 27, 2021 19:10
-
-
Save jeremy-wagner/f9d28c85554b3da01311e9d2fd6d61d8 to your computer and use it in GitHub Desktop.
AWS Lambda handler to add cache-control handler.
This file contains hidden or 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'; | |
// 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