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

Commit b975aed

Browse files
feat(nextjs-cdk-construct): Allow to whitelist headers (#2325)
1 parent 4f93ea7 commit b975aed

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

documentation/docs/cdkconstruct.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ new NextJSLambdaEdge(this, "NextJsApp", {
9292
- `s3Props?: Partial<BucketProps>` - pass custom s3 props
9393
- `whiteListedCookies?: string[]` - provide a list of cookies to forward to the
9494
CloudFront origin.
95+
- `whiteListedHeaders?: string[]` - provide a list of headers to forward to the
96+
CloudFront origin.
9597
- `defaultBehavior?: Partial<cloudfront.Behaviour>` - provide overrides for the
9698
default behavior
9799
- `behaviours?: Array<cloudfront.Behaviour>` - an array of Cloudfront

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ describe("CDK Construct", () => {
6767
});
6868
});
6969

70+
it("lambda cache policy passes correct headers to origin when specified", () => {
71+
const stack = new Stack();
72+
new NextJSLambdaEdge(stack, "Stack", {
73+
serverlessBuildOutDir: path.join(__dirname, "fixtures/app"),
74+
whiteListedHeaders: ["my-header"],
75+
cachePolicyName: {
76+
lambdaCache: "NextLambdaCache"
77+
}
78+
});
79+
80+
const synthesizedStack = SynthUtils.toCloudFormation(stack);
81+
expect(synthesizedStack).toHaveResourceLike(
82+
"AWS::CloudFront::CachePolicy",
83+
{
84+
CachePolicyConfig: {
85+
Name: "NextLambdaCache",
86+
ParametersInCacheKeyAndForwardedToOrigin: {
87+
HeadersConfig: {
88+
HeaderBehavior: "whitelist",
89+
Headers: ["my-header"]
90+
}
91+
}
92+
}
93+
}
94+
);
95+
});
96+
7097
it("lambda cache policy passes correct cookies to origin when specified", () => {
7198
const stack = new Stack();
7299
new NextJSLambdaEdge(stack, "Stack", {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ export class NextJSLambdaEdge extends Construct {
271271
{
272272
cachePolicyName: props.cachePolicyName?.lambdaCache,
273273
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
274-
headerBehavior: cloudfront.CacheHeaderBehavior.none(),
274+
headerBehavior: props.whiteListedHeaders
275+
? cloudfront.CacheHeaderBehavior.allowList(
276+
...props.whiteListedHeaders
277+
)
278+
: cloudfront.CacheHeaderBehavior.none(),
275279
cookieBehavior: {
276280
behavior: props.whiteListedCookies?.length ? "whitelist" : "all",
277281
cookies: props.whiteListedCookies

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export interface Props extends StackProps {
7676
* .e.g ['my-apps-auth-token-cookie-key']
7777
*/
7878
whiteListedCookies?: string[];
79+
80+
/**
81+
* Provide a list of headers to forward to the CloudFront origin.
82+
* This is useful if your SSR page is depending on headers from the request.
83+
*/
84+
whiteListedHeaders?: string[];
85+
7986
/**
8087
* Provide a subset (or all) of the props to override the CloudFront
8188
* distributions default props.

0 commit comments

Comments
 (0)