向 CloudFront Functions 查看器响应事件添加缓存控制标头 - AWS SDK 代码示例

AWS 文档 SDK 示例 GitHub 存储库中还有更多 AWS SDK 示例。

向 CloudFront Functions 查看器响应事件添加缓存控制标头

以下代码示例演示了如何向 CloudFront Functions 查看器响应事件添加缓存控制标头。

JavaScript
适用于 CloudFront Functions 的 JavaScript 运行时 2.0
注意

查看 GitHub,了解更多信息。查找完整示例,并了解如何在 CloudFront Functions examples 存储库中进行设置和运行。

async function handler(event) { var response = event.response; var headers = response.headers; if (response.statusCode >= 200 && response.statusCode < 400) { // Set the cache-control header headers['cache-control'] = {value: 'public, max-age=63072000'}; } // Return response to viewers return response; }