Skip to content

Commit

Permalink
fix: cache policy
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Jul 14, 2024
1 parent a2b2672 commit 4f8716c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-readers-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-docker-cluster": minor
---

The cache policy now defaults to the recommended policy for an Application Load Balancer origin.
5 changes: 5 additions & 0 deletions .changeset/seven-coats-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-site-distribution": minor
---

Custom cache policies are now supported.
5 changes: 5 additions & 0 deletions .changeset/twenty-mails-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-site-distribution": patch
---

Cross origin embedding is now allwed by default.
15 changes: 14 additions & 1 deletion packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
SiteDistributionProps,
} from "@codedazur/cdk-site-distribution";
import { App } from "aws-cdk-lib";
import { OriginProtocolPolicy } from "aws-cdk-lib/aws-cloudfront";
import { CachePolicy, OriginProtocolPolicy } from "aws-cdk-lib/aws-cloudfront";
import { LoadBalancerV2Origin } from "aws-cdk-lib/aws-cloudfront-origins";
import { Platform } from "aws-cdk-lib/aws-ecr-assets";
import {
Expand Down Expand Up @@ -122,11 +122,24 @@ 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 and includes query strings in the
* cache key.
*/
const cachePolicy = CachePolicy.fromCachePolicyId(
this,
"CachePolicy",
"4cc15a8a-d715-48a4-82b8-cc0b614638fe",
);

return new SiteDistribution(this, "Distribution", {
...this.props.distribution,
origin: new LoadBalancerV2Origin(this.service.loadBalancer, {
protocolPolicy: OriginProtocolPolicy.HTTP_ONLY,
}),
cachePolicy,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Distribution,
FunctionCode,
FunctionEventType,
ICachePolicy,
IOrigin,
PriceClass,
ViewerProtocolPolicy,
Expand Down Expand Up @@ -40,6 +41,7 @@ export interface SiteDistributionProps {
subdomain?: string;
zone?: IHostedZone;
};
cachePolicy?: ICachePolicy;
invalidateCache?: boolean | string[];
}

Expand Down Expand Up @@ -258,6 +260,7 @@ export class SiteDistribution extends Construct {
/**
* @todo Make these headers configurable.
* @todo Research CSP and define a good default.
* @todo Enable customizable X-Frame-Options.
*/
protected getSecurityHeadersCode() {
return FunctionCode.fromInline(/* js */ `
Expand All @@ -275,9 +278,9 @@ export class SiteDistribution extends Construct {
value: "nosniff",
};
event.response.headers["x-frame-options"] = {
value: "SAMEORIGIN",
};
// event.response.headers["x-frame-options"] = {
// value: "SAMEORIGIN",
// };
return next(event);
}
Expand Down Expand Up @@ -310,6 +313,7 @@ export class SiteDistribution extends Construct {
]
: []),
],
cachePolicy: this.props.cachePolicy,
},
});

Expand Down

0 comments on commit 4f8716c

Please sign in to comment.