From 0d0aef08afa6dae12fa067b13b710563d0e904e4 Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Fri, 5 Jan 2024 09:41:32 -0300 Subject: [PATCH 1/4] feat: use existing distribution policies when provided --- src/NextjsDistribution.ts | 204 +++++++++++++++++++++----------------- 1 file changed, 115 insertions(+), 89 deletions(-) diff --git a/src/NextjsDistribution.ts b/src/NextjsDistribution.ts index c7c2a996..68f4030e 100644 --- a/src/NextjsDistribution.ts +++ b/src/NextjsDistribution.ts @@ -85,6 +85,10 @@ export interface NextjsDistributionProps { readonly staticAssetsBucket: s3.IBucket; } +type Mutable = { + -readonly [P in keyof T]: T[P]; +}; + /** * Create a CloudFront distribution to serve a Next.js application. */ @@ -175,33 +179,38 @@ export class NextjsDistribution extends Construct { return this.props.functionUrlAuthType === lambda.FunctionUrlAuthType.AWS_IAM; } - private createStaticBehaviorOptions(): cloudfront.BehaviorOptions { - const responseHeadersPolicy = new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', { - // add default header for static assets - customHeadersBehavior: { - customHeaders: [ - { - header: 'cache-control', - override: false, - // MDN Cache-Control Use Case: Caching static assets with "cache busting" - // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets_with_cache_busting - value: `max-age=${Duration.days(365).toSeconds()}, immutable`, - }, - ], - }, - securityHeadersBehavior: this.commonSecurityHeadersBehavior, - comment: 'Nextjs Static Response Headers Policy', - ...this.props.overrides?.staticResponseHeadersPolicyProps, - }); - return { + private createStaticBehaviorOptions(): BehaviorOptions { + const staticBehaviorOptions = >{ ...this.commonBehaviorOptions, origin: this.s3Origin, allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED, - responseHeadersPolicy, ...this.props.overrides?.staticBehaviorOptions, }; + + // add default response headers policy if not provided + if (!staticBehaviorOptions.responseHeadersPolicy) { + staticBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', { + // add default header for static assets + customHeadersBehavior: { + customHeaders: [ + { + header: 'cache-control', + override: false, + // MDN Cache-Control Use Case: Caching static assets with "cache busting" + // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets_with_cache_busting + value: `max-age=${Duration.days(365).toSeconds()}, immutable`, + }, + ], + }, + securityHeadersBehavior: this.commonSecurityHeadersBehavior, + comment: 'Nextjs Static Response Headers Policy', + ...this.props.overrides?.staticResponseHeadersPolicyProps, + }); + } + + return staticBehaviorOptions; } private get fnUrlAuthType(): lambda.FunctionUrlAuthType { @@ -246,52 +255,61 @@ export class NextjsDistribution extends Construct { private createServerBehaviorOptions(): cloudfront.BehaviorOptions { const fnUrl = this.props.serverFunction.addFunctionUrl({ authType: this.fnUrlAuthType }); const origin = new origins.HttpOrigin(Fn.parseDomainName(fnUrl.url), this.props.overrides?.serverHttpOriginProps); - const cachePolicy = new cloudfront.CachePolicy(this, 'ServerCachePolicy', { - queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), - headerBehavior: cloudfront.CacheHeaderBehavior.allowList( - 'accept', - 'rsc', - 'next-router-prefetch', - 'next-router-state-tree', - 'next-url', - 'x-prerender-revalidate' - ), - cookieBehavior: cloudfront.CacheCookieBehavior.all(), - defaultTtl: Duration.seconds(0), - maxTtl: Duration.days(365), - minTtl: Duration.seconds(0), - enableAcceptEncodingBrotli: true, - enableAcceptEncodingGzip: true, - comment: 'Nextjs Server Cache Policy', - ...this.props.overrides?.serverCachePolicyProps, - }); - const responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ServerResponseHeadersPolicy', { - customHeadersBehavior: { - customHeaders: [ - { - header: 'cache-control', - override: false, - // MDN Cache-Control Use Case: Up-to-date contents always - // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always - value: `no-cache`, - }, - ], - }, - securityHeadersBehavior: this.commonSecurityHeadersBehavior, - comment: 'Nextjs Server Response Headers Policy', - ...this.props.overrides?.serverResponseHeadersPolicyProps, - }); - return { + + const serverBehaviorOptions = >{ ...this.commonBehaviorOptions, origin, allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL, originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, - cachePolicy, edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined, functionAssociations: this.createCloudFrontFnAssociations(), - responseHeadersPolicy, ...this.props.overrides?.serverBehaviorOptions, }; + + // add default cache policy if not provided + if (!serverBehaviorOptions.cachePolicy) { + serverBehaviorOptions.cachePolicy = new cloudfront.CachePolicy(this, 'ServerCachePolicy', { + queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), + headerBehavior: cloudfront.CacheHeaderBehavior.allowList( + 'accept', + 'rsc', + 'next-router-prefetch', + 'next-router-state-tree', + 'next-url', + 'x-prerender-revalidate' + ), + cookieBehavior: cloudfront.CacheCookieBehavior.all(), + defaultTtl: Duration.seconds(0), + maxTtl: Duration.days(365), + minTtl: Duration.seconds(0), + enableAcceptEncodingBrotli: true, + enableAcceptEncodingGzip: true, + comment: 'Nextjs Server Cache Policy', + ...this.props.overrides?.serverCachePolicyProps, + }); + } + + // add default response headers policy if not provided + if (!serverBehaviorOptions.responseHeadersPolicy) { + serverBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ServerResponseHeadersPolicy', { + customHeadersBehavior: { + customHeaders: [ + { + header: 'cache-control', + override: false, + // MDN Cache-Control Use Case: Up-to-date contents always + // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always + value: `no-cache`, + }, + ], + }, + securityHeadersBehavior: this.commonSecurityHeadersBehavior, + comment: 'Nextjs Server Response Headers Policy', + ...this.props.overrides?.serverResponseHeadersPolicyProps, + }); + } + + return serverBehaviorOptions; } /** @@ -317,45 +335,53 @@ export class NextjsDistribution extends Construct { Fn.parseDomainName(imageOptFnUrl.url), this.props.overrides?.imageHttpOriginProps ); - const cachePolicy = new cloudfront.CachePolicy(this, 'ImageCachePolicy', { - queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), - headerBehavior: cloudfront.CacheHeaderBehavior.allowList('accept'), - cookieBehavior: cloudfront.CacheCookieBehavior.all(), - defaultTtl: Duration.days(1), - maxTtl: Duration.days(365), - minTtl: Duration.days(0), - enableAcceptEncodingBrotli: true, - enableAcceptEncodingGzip: true, - comment: 'Nextjs Image Cache Policy', - ...this.props.overrides?.imageCachePolicyProps, - }); - const responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ImageResponseHeadersPolicy', { - customHeadersBehavior: { - customHeaders: [ - { - header: 'cache-control', - override: false, - // MDN Cache-Control Use Case: Up-to-date contents always - // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always - value: `no-cache`, - }, - ], - }, - securityHeadersBehavior: this.commonSecurityHeadersBehavior, - comment: 'Nextjs Image Response Headers Policy', - ...this.props.overrides?.imageResponseHeadersPolicyProps, - }); - return { + + const imageBehaviorOptions = >{ ...this.commonBehaviorOptions, origin, allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, - cachePolicy, originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, edgeLambdas: this.edgeLambdas, - responseHeadersPolicy, ...this.props.overrides?.imageBehaviorOptions, }; + + // add default cache policy if not provided + if (!imageBehaviorOptions.cachePolicy) { + imageBehaviorOptions.cachePolicy = new cloudfront.CachePolicy(this, 'ImageCachePolicy', { + queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), + headerBehavior: cloudfront.CacheHeaderBehavior.allowList('accept'), + cookieBehavior: cloudfront.CacheCookieBehavior.all(), + defaultTtl: Duration.days(1), + maxTtl: Duration.days(365), + minTtl: Duration.days(0), + enableAcceptEncodingBrotli: true, + enableAcceptEncodingGzip: true, + comment: 'Nextjs Image Cache Policy', + ...this.props.overrides?.imageCachePolicyProps, + }); + } + + // add default response headers policy if not provided + if (!imageBehaviorOptions.responseHeadersPolicy) { + imageBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ImageResponseHeadersPolicy', { + customHeadersBehavior: { + customHeaders: [ + { + header: 'cache-control', + override: false, + // MDN Cache-Control Use Case: Up-to-date contents always + // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always + value: `no-cache`, + }, + ], + }, + securityHeadersBehavior: this.commonSecurityHeadersBehavior, + comment: 'Nextjs Image Response Headers Policy', + ...this.props.overrides?.imageResponseHeadersPolicyProps, + }); + } + return imageBehaviorOptions; } /** From 1ea20393e7877a27500962b32dccb29fa000094b Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Fri, 5 Jan 2024 09:58:40 -0300 Subject: [PATCH 2/4] use AddBehaviorOptions instead of BehaviorOptions because we cannot override the origin --- src/NextjsDistribution.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/NextjsDistribution.ts b/src/NextjsDistribution.ts index 68f4030e..4e9b5ae9 100644 --- a/src/NextjsDistribution.ts +++ b/src/NextjsDistribution.ts @@ -1,8 +1,12 @@ -import * as fs from 'node:fs'; -import * as path from 'path'; import { Duration, Fn, RemovalPolicy } from 'aws-cdk-lib'; import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; -import { BehaviorOptions, CachePolicyProps, Distribution, ResponseHeadersPolicy } from 'aws-cdk-lib/aws-cloudfront'; +import { + AddBehaviorOptions, + BehaviorOptions, + CachePolicyProps, + Distribution, + ResponseHeadersPolicy, +} from 'aws-cdk-lib/aws-cloudfront'; import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'; import { HttpOriginProps } from 'aws-cdk-lib/aws-cloudfront-origins'; import { PolicyStatement, ServicePrincipal } from 'aws-cdk-lib/aws-iam'; @@ -10,6 +14,8 @@ import * as lambda from 'aws-cdk-lib/aws-lambda'; import { Runtime } from 'aws-cdk-lib/aws-lambda'; import * as s3 from 'aws-cdk-lib/aws-s3'; import { Construct } from 'constructs'; +import * as fs from 'node:fs'; +import * as path from 'path'; import { NEXTJS_BUILD_DIR, NEXTJS_STATIC_DIR } from './constants'; import { OptionalCloudFrontFunctionProps, @@ -25,15 +31,15 @@ export interface NextjsDistributionOverrides { readonly cloudFrontFunctionProps?: OptionalCloudFrontFunctionProps; readonly distributionProps?: OptionalDistributionProps; readonly edgeFunctionProps?: OptionalEdgeFunctionProps; - readonly imageBehaviorOptions?: BehaviorOptions; + readonly imageBehaviorOptions?: AddBehaviorOptions; readonly imageCachePolicyProps?: CachePolicyProps; - readonly imageResponseHeadersPolicyProps: cloudfront.ResponseHeadersPolicyProps; + readonly imageResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps; readonly imageHttpOriginProps?: HttpOriginProps; - readonly serverBehaviorOptions?: BehaviorOptions; + readonly serverBehaviorOptions?: AddBehaviorOptions; readonly serverCachePolicyProps?: CachePolicyProps; readonly serverResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps; readonly serverHttpOriginProps?: HttpOriginProps; - readonly staticBehaviorOptions?: BehaviorOptions; + readonly staticBehaviorOptions?: AddBehaviorOptions; readonly staticResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps; readonly s3OriginProps?: OptionalS3OriginProps; } From 5559f5a472225afa13dc74efc0735155724402e8 Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Fri, 5 Jan 2024 10:04:29 -0300 Subject: [PATCH 3/4] drop Mutable --- API.md | 40 +++++++------- src/NextjsDistribution.ts | 109 +++++++++++++++++++------------------- 2 files changed, 75 insertions(+), 74 deletions(-) diff --git a/API.md b/API.md index dcc834ec..a38d70cf 100644 --- a/API.md +++ b/API.md @@ -2982,33 +2982,23 @@ const nextjsDistributionOverrides: NextjsDistributionOverrides = { ... } | **Name** | **Type** | **Description** | | --- | --- | --- | -| imageResponseHeadersPolicyProps | aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps | *No description.* | | cloudFrontFunctionProps | OptionalCloudFrontFunctionProps | *No description.* | | distributionProps | OptionalDistributionProps | *No description.* | | edgeFunctionProps | OptionalEdgeFunctionProps | *No description.* | -| imageBehaviorOptions | aws-cdk-lib.aws_cloudfront.BehaviorOptions | *No description.* | +| imageBehaviorOptions | aws-cdk-lib.aws_cloudfront.AddBehaviorOptions | *No description.* | | imageCachePolicyProps | aws-cdk-lib.aws_cloudfront.CachePolicyProps | *No description.* | | imageHttpOriginProps | aws-cdk-lib.aws_cloudfront_origins.HttpOriginProps | *No description.* | +| imageResponseHeadersPolicyProps | aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps | *No description.* | | s3OriginProps | OptionalS3OriginProps | *No description.* | -| serverBehaviorOptions | aws-cdk-lib.aws_cloudfront.BehaviorOptions | *No description.* | +| serverBehaviorOptions | aws-cdk-lib.aws_cloudfront.AddBehaviorOptions | *No description.* | | serverCachePolicyProps | aws-cdk-lib.aws_cloudfront.CachePolicyProps | *No description.* | | serverHttpOriginProps | aws-cdk-lib.aws_cloudfront_origins.HttpOriginProps | *No description.* | | serverResponseHeadersPolicyProps | aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps | *No description.* | -| staticBehaviorOptions | aws-cdk-lib.aws_cloudfront.BehaviorOptions | *No description.* | +| staticBehaviorOptions | aws-cdk-lib.aws_cloudfront.AddBehaviorOptions | *No description.* | | staticResponseHeadersPolicyProps | aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps | *No description.* | --- -##### `imageResponseHeadersPolicyProps`Required - -```typescript -public readonly imageResponseHeadersPolicyProps: ResponseHeadersPolicyProps; -``` - -- *Type:* aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps - ---- - ##### `cloudFrontFunctionProps`Optional ```typescript @@ -3042,10 +3032,10 @@ public readonly edgeFunctionProps: OptionalEdgeFunctionProps; ##### `imageBehaviorOptions`Optional ```typescript -public readonly imageBehaviorOptions: BehaviorOptions; +public readonly imageBehaviorOptions: AddBehaviorOptions; ``` -- *Type:* aws-cdk-lib.aws_cloudfront.BehaviorOptions +- *Type:* aws-cdk-lib.aws_cloudfront.AddBehaviorOptions --- @@ -3069,6 +3059,16 @@ public readonly imageHttpOriginProps: HttpOriginProps; --- +##### `imageResponseHeadersPolicyProps`Optional + +```typescript +public readonly imageResponseHeadersPolicyProps: ResponseHeadersPolicyProps; +``` + +- *Type:* aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicyProps + +--- + ##### `s3OriginProps`Optional ```typescript @@ -3082,10 +3082,10 @@ public readonly s3OriginProps: OptionalS3OriginProps; ##### `serverBehaviorOptions`Optional ```typescript -public readonly serverBehaviorOptions: BehaviorOptions; +public readonly serverBehaviorOptions: AddBehaviorOptions; ``` -- *Type:* aws-cdk-lib.aws_cloudfront.BehaviorOptions +- *Type:* aws-cdk-lib.aws_cloudfront.AddBehaviorOptions --- @@ -3122,10 +3122,10 @@ public readonly serverResponseHeadersPolicyProps: ResponseHeadersPolicyProps; ##### `staticBehaviorOptions`Optional ```typescript -public readonly staticBehaviorOptions: BehaviorOptions; +public readonly staticBehaviorOptions: AddBehaviorOptions; ``` -- *Type:* aws-cdk-lib.aws_cloudfront.BehaviorOptions +- *Type:* aws-cdk-lib.aws_cloudfront.AddBehaviorOptions --- diff --git a/src/NextjsDistribution.ts b/src/NextjsDistribution.ts index 4e9b5ae9..396e02df 100644 --- a/src/NextjsDistribution.ts +++ b/src/NextjsDistribution.ts @@ -1,3 +1,5 @@ +import * as fs from 'node:fs'; +import * as path from 'path'; import { Duration, Fn, RemovalPolicy } from 'aws-cdk-lib'; import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; import { @@ -14,8 +16,6 @@ import * as lambda from 'aws-cdk-lib/aws-lambda'; import { Runtime } from 'aws-cdk-lib/aws-lambda'; import * as s3 from 'aws-cdk-lib/aws-s3'; import { Construct } from 'constructs'; -import * as fs from 'node:fs'; -import * as path from 'path'; import { NEXTJS_BUILD_DIR, NEXTJS_STATIC_DIR } from './constants'; import { OptionalCloudFrontFunctionProps, @@ -91,10 +91,6 @@ export interface NextjsDistributionProps { readonly staticAssetsBucket: s3.IBucket; } -type Mutable = { - -readonly [P in keyof T]: T[P]; -}; - /** * Create a CloudFront distribution to serve a Next.js application. */ @@ -186,18 +182,12 @@ export class NextjsDistribution extends Construct { } private createStaticBehaviorOptions(): BehaviorOptions { - const staticBehaviorOptions = >{ - ...this.commonBehaviorOptions, - origin: this.s3Origin, - allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, - cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, - cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED, - ...this.props.overrides?.staticBehaviorOptions, - }; + const staticBehaviorOptions = this.props.overrides?.staticBehaviorOptions; - // add default response headers policy if not provided - if (!staticBehaviorOptions.responseHeadersPolicy) { - staticBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', { + // create default response headers policy if not provided + const responseHeadersPolicy = + staticBehaviorOptions?.responseHeadersPolicy ?? + new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', { // add default header for static assets customHeadersBehavior: { customHeaders: [ @@ -214,9 +204,16 @@ export class NextjsDistribution extends Construct { comment: 'Nextjs Static Response Headers Policy', ...this.props.overrides?.staticResponseHeadersPolicyProps, }); - } - return staticBehaviorOptions; + return { + ...this.commonBehaviorOptions, + origin: this.s3Origin, + allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, + cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, + cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED, + ...staticBehaviorOptions, + responseHeadersPolicy, + }; } private get fnUrlAuthType(): lambda.FunctionUrlAuthType { @@ -261,20 +258,12 @@ export class NextjsDistribution extends Construct { private createServerBehaviorOptions(): cloudfront.BehaviorOptions { const fnUrl = this.props.serverFunction.addFunctionUrl({ authType: this.fnUrlAuthType }); const origin = new origins.HttpOrigin(Fn.parseDomainName(fnUrl.url), this.props.overrides?.serverHttpOriginProps); + const serverBehaviorOptions = this.props.overrides?.serverBehaviorOptions; - const serverBehaviorOptions = >{ - ...this.commonBehaviorOptions, - origin, - allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL, - originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, - edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined, - functionAssociations: this.createCloudFrontFnAssociations(), - ...this.props.overrides?.serverBehaviorOptions, - }; - - // add default cache policy if not provided - if (!serverBehaviorOptions.cachePolicy) { - serverBehaviorOptions.cachePolicy = new cloudfront.CachePolicy(this, 'ServerCachePolicy', { + // create default cache policy if not provided + const cachePolicy = + serverBehaviorOptions?.cachePolicy ?? + new cloudfront.CachePolicy(this, 'ServerCachePolicy', { queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), headerBehavior: cloudfront.CacheHeaderBehavior.allowList( 'accept', @@ -293,11 +282,11 @@ export class NextjsDistribution extends Construct { comment: 'Nextjs Server Cache Policy', ...this.props.overrides?.serverCachePolicyProps, }); - } - // add default response headers policy if not provided - if (!serverBehaviorOptions.responseHeadersPolicy) { - serverBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ServerResponseHeadersPolicy', { + // create default response headers policy if not provided + const responseHeadersPolicy = + serverBehaviorOptions?.responseHeadersPolicy ?? + new ResponseHeadersPolicy(this, 'ServerResponseHeadersPolicy', { customHeadersBehavior: { customHeaders: [ { @@ -313,9 +302,18 @@ export class NextjsDistribution extends Construct { comment: 'Nextjs Server Response Headers Policy', ...this.props.overrides?.serverResponseHeadersPolicyProps, }); - } - return serverBehaviorOptions; + return { + ...this.commonBehaviorOptions, + origin, + allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL, + originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, + edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined, + functionAssociations: this.createCloudFrontFnAssociations(), + ...serverBehaviorOptions, + cachePolicy, + responseHeadersPolicy, + }; } /** @@ -342,19 +340,12 @@ export class NextjsDistribution extends Construct { this.props.overrides?.imageHttpOriginProps ); - const imageBehaviorOptions = >{ - ...this.commonBehaviorOptions, - origin, - allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, - cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, - originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, - edgeLambdas: this.edgeLambdas, - ...this.props.overrides?.imageBehaviorOptions, - }; + const imageBehaviorOptions = this.props.overrides?.imageBehaviorOptions; // add default cache policy if not provided - if (!imageBehaviorOptions.cachePolicy) { - imageBehaviorOptions.cachePolicy = new cloudfront.CachePolicy(this, 'ImageCachePolicy', { + const cachePolicy = + imageBehaviorOptions?.cachePolicy ?? + new cloudfront.CachePolicy(this, 'ImageCachePolicy', { queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(), headerBehavior: cloudfront.CacheHeaderBehavior.allowList('accept'), cookieBehavior: cloudfront.CacheCookieBehavior.all(), @@ -366,11 +357,11 @@ export class NextjsDistribution extends Construct { comment: 'Nextjs Image Cache Policy', ...this.props.overrides?.imageCachePolicyProps, }); - } // add default response headers policy if not provided - if (!imageBehaviorOptions.responseHeadersPolicy) { - imageBehaviorOptions.responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ImageResponseHeadersPolicy', { + const responseHeadersPolicy = + imageBehaviorOptions?.responseHeadersPolicy ?? + new ResponseHeadersPolicy(this, 'ImageResponseHeadersPolicy', { customHeadersBehavior: { customHeaders: [ { @@ -386,8 +377,18 @@ export class NextjsDistribution extends Construct { comment: 'Nextjs Image Response Headers Policy', ...this.props.overrides?.imageResponseHeadersPolicyProps, }); - } - return imageBehaviorOptions; + + return { + ...this.commonBehaviorOptions, + origin, + allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, + cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, + originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, + edgeLambdas: this.edgeLambdas, + ...imageBehaviorOptions, + cachePolicy, + responseHeadersPolicy, + }; } /** From 62b293b31ad9f3b032602fb5bc17ef081e552052 Mon Sep 17 00:00:00 2001 From: Marcelo Luiz Onhate Date: Fri, 5 Jan 2024 10:33:12 -0300 Subject: [PATCH 4/4] ...spread after --- src/NextjsDistribution.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NextjsDistribution.ts b/src/NextjsDistribution.ts index 396e02df..d265b8e6 100644 --- a/src/NextjsDistribution.ts +++ b/src/NextjsDistribution.ts @@ -211,8 +211,8 @@ export class NextjsDistribution extends Construct { allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS, cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED, - ...staticBehaviorOptions, responseHeadersPolicy, + ...staticBehaviorOptions, }; } @@ -310,9 +310,9 @@ export class NextjsDistribution extends Construct { originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined, functionAssociations: this.createCloudFrontFnAssociations(), - ...serverBehaviorOptions, cachePolicy, responseHeadersPolicy, + ...serverBehaviorOptions, }; } @@ -385,9 +385,9 @@ export class NextjsDistribution extends Construct { cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS, originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, edgeLambdas: this.edgeLambdas, - ...imageBehaviorOptions, cachePolicy, responseHeadersPolicy, + ...imageBehaviorOptions, }; }