Skip to content

Commit

Permalink
feat(nextjs-cdk-construct): Allow cache policies to be provided as props
Browse files Browse the repository at this point in the history
  • Loading branch information
jadenv committed Feb 16, 2022
1 parent ecb03a6 commit 000d0ef
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
3 changes: 3 additions & 0 deletions documentation/docs/cdkconstruct.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ new NextJSLambdaEdge(this, "NextJsApp", {
- `invalidationPaths?: string[]` - an array of invalidation paths, by default we
invalidate all pages found in manifest
- `cachePolicyName?: Object`: configure the name given to the cache policies
- `nextStaticsCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for static resources
- `nextImageCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for image caching
- `nextLambdaCachePolicy?: CachePolicy;`: configure the CloudFront cache policy used for Lambda functions
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NextJSLambdaEdge } from "../src";
import { Runtime, Function, Code } from "aws-cdk-lib/aws-lambda";
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
import { HostedZone } from "aws-cdk-lib/aws-route53";
import { LambdaEdgeEventType } from "aws-cdk-lib/aws-cloudfront";
import { LambdaEdgeEventType, CachePolicy } from "aws-cdk-lib/aws-cloudfront";

describe("CDK Construct", () => {
it("passes correct lambda options to underlying lambdas when single value passed", () => {
Expand Down Expand Up @@ -146,6 +146,66 @@ describe("CDK Construct", () => {
);
});

it("statics cache policy uses passed in policy if provided", () => {
const stack = new Stack();
new NextJSLambdaEdge(stack, "Stack", {
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
nextStaticsCachePolicy: new CachePolicy(stack, "NextStaticsCache", {
cachePolicyName: "customNextStaticsCache"
})
});

const synthesizedStack = SynthUtils.toCloudFormation(stack);
expect(synthesizedStack).toHaveResourceLike(
"AWS::CloudFront::CachePolicy",
{
CachePolicyConfig: {
Name: "customNextStaticsCache"
}
}
);
});

it("image cache policy uses passed in policy if provided", () => {
const stack = new Stack();
new NextJSLambdaEdge(stack, "Stack", {
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
nextImageCachePolicy: new CachePolicy(stack, "NextImageCache", {
cachePolicyName: "customNextImageCache"
})
});

const synthesizedStack = SynthUtils.toCloudFormation(stack);
expect(synthesizedStack).toHaveResourceLike(
"AWS::CloudFront::CachePolicy",
{
CachePolicyConfig: {
Name: "customNextImageCache"
}
}
);
});

it("lambda cache policy uses passed in policy if provided", () => {
const stack = new Stack();
new NextJSLambdaEdge(stack, "Stack", {
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
nextLambdaCachePolicy: new CachePolicy(stack, "NextLambdaCache", {
cachePolicyName: "customNextLambdaCache"
})
});

const synthesizedStack = SynthUtils.toCloudFormation(stack);
expect(synthesizedStack).toHaveResourceLike(
"AWS::CloudFront::CachePolicy",
{
CachePolicyConfig: {
Name: "customNextLambdaCache"
}
}
);
});

it("creates resources required for a custom domain when specified", () => {
const stack = new Stack();
const certificate = Certificate.fromCertificateArn(
Expand Down
21 changes: 9 additions & 12 deletions packages/serverless-components/nextjs-cdk-construct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ export class NextJSLambdaEdge extends Construct {
this.nextImageLambda.currentVersion.addAlias("live");
}

this.nextStaticsCachePolicy = new cloudfront.CachePolicy(
this,
"NextStaticsCache",
{
this.nextStaticsCachePolicy =
props.nextStaticsCachePolicy ||
new cloudfront.CachePolicy(this, "NextStaticsCache", {
cachePolicyName: props.cachePolicyName?.staticsCache,
queryStringBehavior: cloudfront.CacheQueryStringBehavior.none(),
headerBehavior: cloudfront.CacheHeaderBehavior.none(),
Expand All @@ -249,10 +248,9 @@ export class NextJSLambdaEdge extends Construct {
}
);

this.nextImageCachePolicy = new cloudfront.CachePolicy(
this,
"NextImageCache",
{
this.nextImageCachePolicy =
props.nextImageCachePolicy ||
new cloudfront.CachePolicy(this, "NextImageCache", {
cachePolicyName: props.cachePolicyName?.imageCache,
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
headerBehavior: cloudfront.CacheHeaderBehavior.allowList("Accept"),
Expand All @@ -265,10 +263,9 @@ export class NextJSLambdaEdge extends Construct {
}
);

this.nextLambdaCachePolicy = new cloudfront.CachePolicy(
this,
"NextLambdaCache",
{
this.nextLambdaCachePolicy =
props.nextLambdaCachePolicy ||
new cloudfront.CachePolicy(this, "NextLambdaCache", {
cachePolicyName: props.cachePolicyName?.lambdaCache,
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
headerBehavior: props.whiteListedHeaders
Expand Down
18 changes: 17 additions & 1 deletion packages/serverless-components/nextjs-cdk-construct/src/props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
import { BehaviorOptions, DistributionProps } from "aws-cdk-lib/aws-cloudfront";
import { BehaviorOptions, DistributionProps, CachePolicy } from "aws-cdk-lib/aws-cloudfront";
import { Runtime } from "aws-cdk-lib/aws-lambda";
import { IHostedZone } from "aws-cdk-lib/aws-route53";
import { BucketProps } from "aws-cdk-lib/aws-s3";
Expand Down Expand Up @@ -108,4 +108,20 @@ export interface Props extends StackProps {
* Override props passed to the underlying s3 bucket
*/
cloudfrontProps?: Partial<DistributionProps>;

/**
* Override cache policy used for statics
*/
nextStaticsCachePolicy?: CachePolicy;

/**
* Override cache policy used for image caching
*/

nextImageCachePolicy?: CachePolicy;

/**
* Override cache policy used for Lambda
*/
nextLambdaCachePolicy?: CachePolicy;
}

0 comments on commit 000d0ef

Please sign in to comment.