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

Commit 9885900

Browse files
better docs and add input to component
1 parent 8b1d501 commit 9885900

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ The fourth cache behaviour handles next API requests `api/*`.
430430
| useServerlessTraceTarget | `boolean` | `false` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. |
431431
| logLambdaExecutionTimes | `boolean` | `false` | Logs to CloudWatch the default handler performance metrics. |
432432
| minifyHandlers | `boolean` | `false` | Use minified handlers to reduce code size. |
433+
| enableHTTPCompression | `boolean` | `false` | When set to `true` the Lambda@Edge functions for SSR and API requests will use Gzip to compress the response. Note that you shouldn't need to enable this because CloudFront will compress responses for you out of the box. |
433434

434435
Custom inputs can be configured like this:
435436

packages/compat-layers/lambda-at-edge-compat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ module.exports.render = async (event, context) => {
2727

2828
```js
2929
const { req, res, responsePromise } = cloudFrontCompat(event.Records[0].cf, {
30-
enableHTTPCompresssion: true // false by default
30+
enableHTTPCompression: true // false by default
3131
});
3232
```

packages/compat-layers/lambda-at-edge-compat/__tests__/next-aws-cloudfront.response.test.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,10 @@ describe("Response Tests", () => {
371371
});
372372
});
373373

374-
it(`gzips`, () => {
375-
expect.assertions(2);
374+
it("does not gzip by default", () => {
375+
expect.assertions(3);
376376

377377
const gzipSpy = jest.spyOn(zlib, "gzipSync");
378-
gzipSpy.mockReturnValueOnce(Buffer.from("ok-gzipped"));
379378

380379
const { res, responsePromise } = create({
381380
request: {
@@ -393,6 +392,40 @@ describe("Response Tests", () => {
393392

394393
res.end("ok");
395394

395+
return responsePromise.then((response) => {
396+
expect(gzipSpy).not.toBeCalled();
397+
expect(response.headers["content-encoding"]).not.toBeDefined();
398+
expect(response.body).toEqual("b2s=");
399+
});
400+
});
401+
402+
it(`gzips when compression is enabled`, () => {
403+
expect.assertions(2);
404+
405+
const gzipSpy = jest.spyOn(zlib, "gzipSync");
406+
gzipSpy.mockReturnValueOnce(Buffer.from("ok-gzipped"));
407+
408+
const { res, responsePromise } = create(
409+
{
410+
request: {
411+
path: "/",
412+
headers: {
413+
"accept-encoding": [
414+
{
415+
key: "Accept-Encoding",
416+
value: "gzip"
417+
}
418+
]
419+
}
420+
}
421+
},
422+
{
423+
enableHTTPCompression: true
424+
}
425+
);
426+
427+
res.end("ok");
428+
396429
gzipSpy.mockRestore();
397430

398431
return responsePromise.then((response) => {

packages/compat-layers/lambda-at-edge-compat/next-aws-cloudfront.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ const isGzipSupported = (headers) => {
144144
return gz;
145145
};
146146

147-
const handler = (event, { enableHTTPCompression }) => {
147+
const defaultOptions = {
148+
enableHTTPCompression: false
149+
};
150+
151+
const handler = (event, { enableHTTPCompression } = defaultOptions) => {
148152
const { request: cfRequest, response: cfResponse = { headers: {} } } = event;
149153

150154
const response = {

packages/serverless-components/nextjs-component/src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class NextjsComponent extends Component {
188188
logLambdaExecutionTimes: inputs.logLambdaExecutionTimes || false,
189189
domainRedirects: inputs.domainRedirects || {},
190190
minifyHandlers: inputs.minifyHandlers || false,
191-
enableHTTPCompression: false
191+
enableHTTPCompression: inputs.enableHTTPCompression || false
192192
}
193193
);
194194

packages/serverless-components/nextjs-component/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type ServerlessComponentInputs = {
2020
domainRedirects?: { [key: string]: string };
2121
cloudfront?: CloudfrontOptions;
2222
minifyHandlers?: boolean;
23+
enableHTTPCompression?: boolean;
2324
};
2425

2526
type CloudfrontOptions = Record<string, any>;

0 commit comments

Comments
 (0)