Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 68b7717

Browse files
authored
feat(nextjs-cdk-construct): allow cache policies to be provided as props (#2350)
1 parent 20dbf87 commit 68b7717

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

documentation/docs/cdkconstruct.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,6 @@ new NextJSLambdaEdge(this, "NextJsApp", {
101101
- `invalidationPaths?: string[]` - an array of invalidation paths, by default we
102102
invalidate all pages found in manifest
103103
- `cachePolicyName?: Object`: configure the name given to the cache policies
104+
- `nextStaticsCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for static resources
105+
- `nextImageCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for image caching
106+
- `nextLambdaCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for Lambda functions

packages/serverless-components/nextjs-cdk-construct/__tests__/construct.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { NextJSLambdaEdge } from "../src";
66
import { Runtime, Function, Code } from "aws-cdk-lib/aws-lambda";
77
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
88
import { HostedZone } from "aws-cdk-lib/aws-route53";
9-
import { LambdaEdgeEventType } from "aws-cdk-lib/aws-cloudfront";
9+
import { LambdaEdgeEventType, CachePolicy } from "aws-cdk-lib/aws-cloudfront";
1010

1111
describe("CDK Construct", () => {
1212
it("passes correct lambda options to underlying lambdas when single value passed", () => {
@@ -146,6 +146,66 @@ describe("CDK Construct", () => {
146146
);
147147
});
148148

149+
it("statics cache policy uses passed in policy if provided", () => {
150+
const stack = new Stack();
151+
new NextJSLambdaEdge(stack, "Stack", {
152+
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
153+
nextStaticsCachePolicy: new CachePolicy(stack, "NextStaticsCache", {
154+
cachePolicyName: "customNextStaticsCache"
155+
})
156+
});
157+
158+
const synthesizedStack = SynthUtils.toCloudFormation(stack);
159+
expect(synthesizedStack).toHaveResourceLike(
160+
"AWS::CloudFront::CachePolicy",
161+
{
162+
CachePolicyConfig: {
163+
Name: "customNextStaticsCache"
164+
}
165+
}
166+
);
167+
});
168+
169+
it("image cache policy uses passed in policy if provided", () => {
170+
const stack = new Stack();
171+
new NextJSLambdaEdge(stack, "Stack", {
172+
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
173+
nextImageCachePolicy: new CachePolicy(stack, "NextImageCache", {
174+
cachePolicyName: "customNextImageCache"
175+
})
176+
});
177+
178+
const synthesizedStack = SynthUtils.toCloudFormation(stack);
179+
expect(synthesizedStack).toHaveResourceLike(
180+
"AWS::CloudFront::CachePolicy",
181+
{
182+
CachePolicyConfig: {
183+
Name: "customNextImageCache"
184+
}
185+
}
186+
);
187+
});
188+
189+
it("lambda cache policy uses passed in policy if provided", () => {
190+
const stack = new Stack();
191+
new NextJSLambdaEdge(stack, "Stack", {
192+
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
193+
nextLambdaCachePolicy: new CachePolicy(stack, "NextLambdaCache", {
194+
cachePolicyName: "customNextLambdaCache"
195+
})
196+
});
197+
198+
const synthesizedStack = SynthUtils.toCloudFormation(stack);
199+
expect(synthesizedStack).toHaveResourceLike(
200+
"AWS::CloudFront::CachePolicy",
201+
{
202+
CachePolicyConfig: {
203+
Name: "customNextLambdaCache"
204+
}
205+
}
206+
);
207+
});
208+
149209
it("creates resources required for a custom domain when specified", () => {
150210
const stack = new Stack();
151211
const certificate = Certificate.fromCertificateArn(

packages/serverless-components/nextjs-cdk-construct/src/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,9 @@ export class NextJSLambdaEdge extends Construct {
233233
this.nextImageLambda.currentVersion.addAlias("live");
234234
}
235235

236-
this.nextStaticsCachePolicy = new cloudfront.CachePolicy(
237-
this,
238-
"NextStaticsCache",
239-
{
236+
this.nextStaticsCachePolicy =
237+
props.nextStaticsCachePolicy ||
238+
new cloudfront.CachePolicy(this, "NextStaticsCache", {
240239
cachePolicyName: props.cachePolicyName?.staticsCache,
241240
queryStringBehavior: cloudfront.CacheQueryStringBehavior.none(),
242241
headerBehavior: cloudfront.CacheHeaderBehavior.none(),
@@ -249,10 +248,9 @@ export class NextJSLambdaEdge extends Construct {
249248
}
250249
);
251250

252-
this.nextImageCachePolicy = new cloudfront.CachePolicy(
253-
this,
254-
"NextImageCache",
255-
{
251+
this.nextImageCachePolicy =
252+
props.nextImageCachePolicy ||
253+
new cloudfront.CachePolicy(this, "NextImageCache", {
256254
cachePolicyName: props.cachePolicyName?.imageCache,
257255
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
258256
headerBehavior: cloudfront.CacheHeaderBehavior.allowList("Accept"),
@@ -265,10 +263,9 @@ export class NextJSLambdaEdge extends Construct {
265263
}
266264
);
267265

268-
this.nextLambdaCachePolicy = new cloudfront.CachePolicy(
269-
this,
270-
"NextLambdaCache",
271-
{
266+
this.nextLambdaCachePolicy =
267+
props.nextLambdaCachePolicy ||
268+
new cloudfront.CachePolicy(this, "NextLambdaCache", {
272269
cachePolicyName: props.cachePolicyName?.lambdaCache,
273270
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
274271
headerBehavior: props.whiteListedHeaders

packages/serverless-components/nextjs-cdk-construct/src/props.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
2-
import { BehaviorOptions, DistributionProps } from "aws-cdk-lib/aws-cloudfront";
2+
import { BehaviorOptions, DistributionProps, CachePolicy } from "aws-cdk-lib/aws-cloudfront";
33
import { Runtime } from "aws-cdk-lib/aws-lambda";
44
import { IHostedZone } from "aws-cdk-lib/aws-route53";
55
import { BucketProps } from "aws-cdk-lib/aws-s3";
@@ -108,4 +108,20 @@ export interface Props extends StackProps {
108108
* Override props passed to the underlying s3 bucket
109109
*/
110110
cloudfrontProps?: Partial<DistributionProps>;
111+
112+
/**
113+
* Override cache policy used for statics
114+
*/
115+
nextStaticsCachePolicy?: CachePolicy;
116+
117+
/**
118+
* Override cache policy used for image caching
119+
*/
120+
121+
nextImageCachePolicy?: CachePolicy;
122+
123+
/**
124+
* Override cache policy used for Lambda
125+
*/
126+
nextLambdaCachePolicy?: CachePolicy;
111127
}

0 commit comments

Comments
 (0)