

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 向 CloudFront 函数查看器响应事件添加缓存控制标头
<a name="cloudfront_example_cloudfront_functions_add_cache_control_header_section"></a>

以下代码示例说明如何向 CloudFront 函数查看器响应事件添加缓存控制标头。

------
#### [ JavaScript ]

**JavaScript CloudFront 函数的运行时 2.0**  
 还有更多相关信息 GitHub。在 Functions 示例存储库中查找完整的[示例](https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-cache-control-header)并学习如何设置和运行。CloudFront 

```
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;
}
```

------