Skip to content

Commit

Permalink
fix: #118 allow to customize the static response headers policy (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarS authored Oct 25, 2023
1 parent e68e49f commit 15deeb7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
11 changes: 11 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface NextjsDistributionCdkProps {
}

export interface NextjsCachePolicyProps {
readonly staticResponseHeaderPolicy?: ResponseHeadersPolicy;
readonly staticCachePolicy?: cloudfront.ICachePolicy;
readonly serverCachePolicy?: cloudfront.ICachePolicy;
readonly imageCachePolicy?: cloudfront.ICachePolicy;
Expand Down Expand Up @@ -296,20 +297,22 @@ export class NextjsDistribution extends Construct {
private createStaticBehaviorOptions(): cloudfront.BehaviorOptions {
const staticClientMaxAge = this.props.cachePolicies?.staticClientMaxAgeDefault || DEFAULT_STATIC_MAX_AGE;
// TODO: remove this response headers policy once S3 files have correct cache control headers with new asset deployment technique
const responseHeadersPolicy = new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', {
// add default header for static assets
customHeadersBehavior: {
customHeaders: [
{
header: 'cache-control',
override: false,
// by default tell browser to cache static files for this long
// this is separate from the origin cache policy
value: `public,max-age=${staticClientMaxAge},immutable`,
},
],
},
});
const responseHeadersPolicy =
this.props.cachePolicies?.staticResponseHeaderPolicy ??
new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', {
// add default header for static assets
customHeadersBehavior: {
customHeaders: [
{
header: 'cache-control',
override: false,
// by default tell browser to cache static files for this long
// this is separate from the origin cache policy
value: `public,max-age=${staticClientMaxAge},immutable`,
},
],
},
});
const cachePolicy = this.props.cachePolicies?.staticCachePolicy ?? cloudfront.CachePolicy.CACHING_OPTIMIZED;
return {
...this.commonBehaviorOptions,
Expand Down

0 comments on commit 15deeb7

Please sign in to comment.