From 55ddf9b6d47be519a7ad7a932d20ec1526b5d1e2 Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Sun, 1 Sep 2024 03:54:07 +0200 Subject: [PATCH] fix: next app api behavior --- .changeset/thirty-oranges-punch.md | 5 ++ .changeset/two-pillows-walk.md | 5 ++ .../src/constructs/DockerCluster.ts | 50 ++++++++----------- .../cdk-next-app/src/constructs/NextApp.ts | 9 ++-- 4 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 .changeset/thirty-oranges-punch.md create mode 100644 .changeset/two-pillows-walk.md diff --git a/.changeset/thirty-oranges-punch.md b/.changeset/thirty-oranges-punch.md new file mode 100644 index 00000000..3a5c8bed --- /dev/null +++ b/.changeset/thirty-oranges-punch.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-next-app": patch +--- + +The default api behavior is no longer overridden. diff --git a/.changeset/two-pillows-walk.md b/.changeset/two-pillows-walk.md new file mode 100644 index 00000000..8f89ebef --- /dev/null +++ b/.changeset/two-pillows-walk.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-docker-cluster": patch +--- + +THe default cache and origin request policies were simplified. diff --git a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts index 1a5bcfaa..84e36df0 100644 --- a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts +++ b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts @@ -5,9 +5,7 @@ import { import { App } from "aws-cdk-lib"; import { AllowedMethods, - CachePolicy, OriginProtocolPolicy, - OriginRequestPolicy, } from "aws-cdk-lib/aws-cloudfront"; import { LoadBalancerV2Origin } from "aws-cdk-lib/aws-cloudfront-origins"; import { Platform } from "aws-cdk-lib/aws-ecr-assets"; @@ -129,35 +127,29 @@ export class DockerCluster extends Construct { } protected createDistribution() { - /** - * This retrieves the managed "UseOriginCacheControlHeaders-QueryStrings" - * cache policy, which is designed for use with an origin that sends - * Cache-Control headers with the object, which is recommended for use with - * an Application Load Balancer, and includes query strings in the cache - * key. - */ - const cachePolicy = CachePolicy.fromCachePolicyId( - this, - "CachePolicy", - "4cc15a8a-d715-48a4-82b8-cc0b614638fe", - ); - - /** - * This retrieves the managed "AllViewer" origin request policy, which - * includes all values (query strings, headers, and cookies) in the viewer - * request, which is recommended for use with an Application Load Balancer - * endpoint. - */ - const originRequestPolicy = OriginRequestPolicy.fromOriginRequestPolicyId( - this, - "OriginRequestPolicy", - "216adef6-5c7f-47e4-b989-5492eafa07d3", - ); - return new SiteDistribution(this, "Distribution", { allowedMethods: AllowedMethods.ALLOW_ALL, - cachePolicy, - originRequestPolicy, + cachePolicy: { + /** + * The managed "UseOriginCacheControlHeaders-QueryStrings" cache policy, + * which is designed for use with an origin that sends Cache-Control + * headers with the object, which is recommended for use with an + * Application Load Balancer, and includes query strings in the cache + * key. + * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html#managed-cache-policy-origin-cache-headers-query-strings + */ + cachePolicyId: "4cc15a8a-d715-48a4-82b8-cc0b614638fe", + }, + originRequestPolicy: { + /** + * The managed "AllViewer" origin request policy, which includes all + * values (query strings, headers, and cookies) in the viewer request, + * which is recommended for use with an Application Load Balancer + * endpoint. + * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html#managed-origin-request-policy-all-viewer + */ + originRequestPolicyId: "216adef6-5c7f-47e4-b989-5492eafa07d3", + }, ...this.props.distribution, origin: new LoadBalancerV2Origin(this.service.loadBalancer, { protocolPolicy: OriginProtocolPolicy.HTTP_ONLY, diff --git a/packages/cdk-next-app/src/constructs/NextApp.ts b/packages/cdk-next-app/src/constructs/NextApp.ts index b5ffd5ed..19f4ffd4 100644 --- a/packages/cdk-next-app/src/constructs/NextApp.ts +++ b/packages/cdk-next-app/src/constructs/NextApp.ts @@ -25,26 +25,27 @@ export class NextApp extends DockerCluster { { source, service: { port = 3000, ...service } = {}, + distribution, ...props }: NextAppProps, ) { super(scope, id, { source: NextApp.withNpmSecret(source), + ...props, service: { port, ...service, }, distribution: { - ...props.distribution, + ...distribution, behaviors: { - ...props.distribution?.behaviors, + ...distribution?.behaviors, "/api/*": { authentication: false, - ...props.distribution?.behaviors?.["/api/*"], + ...distribution?.behaviors?.["/api/*"], }, }, }, - ...props, }); }