The following viewer response function adds a Cache-Control
HTTP header
to the response. The header uses the max-age
directive to tell web browsers
to cache the response for a maximum of two years (63,072,000 seconds). For more
information, see Cache-Control on the MDN Web Docs website.
See this example on GitHub.
- JavaScript runtime 2.0
-
async function handler(event) {
const response = event.response;
const headers = response.headers;
// Set the cache-control header
headers['cache-control'] = {value: 'public, max-age=63072000'};
// Return response to viewers
return response;
}
- JavaScript runtime 1.0
-
function handler(event) {
var response = event.response;
var headers = response.headers;
// Set the cache-control header
headers['cache-control'] = {value: 'public, max-age=63072000'};
// Return response to viewers
return response;
}