

# CloudFront Functions ビューワーレスポンスイベントにキャッシュコントロールヘッダーを追加する
<a name="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;
}
```

------

AWS SDK デベロッパーガイドとコード例の詳細なリストについては、[AWS SDK での CloudFront の使用](sdk-general-information-section.md) を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。