

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將快取控制標頭新增至 CloudFront Functions 檢視器回應事件
<a name="cloudfront_example_cloudfront_functions_add_cache_control_header_section"></a>

下列程式碼範例示範如何將快取控制標頭新增至 CloudFront Functions 檢視器回應事件。

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

**適用於 CloudFront Functions 的 JavaScript 執行時期 2.0**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [CloudFront Functions 範例](https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-cache-control-header)儲存庫中設定和執行。

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

------