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

Let CloudFront do the Gzipping #692

Merged
merged 5 commits into from
Oct 22, 2020
Merged

Conversation

danielcondemarin
Copy link
Contributor

@danielcondemarin danielcondemarin commented Oct 18, 2020

Motivation

We've been manually doing Gzip compression in lambda@edge so far due to CloudFront not compressing lambda@edge responses. However, seems like that has been fixed after discussing with Cristian from the CloudFront team and doing some testing of my own.

This should allow us to use Gzip and Brotli compression without extra code in the Lambda@Edge handler. Furthermore the CloudFront distributions created by serverless-next.js already enable compression by default.

From my initial findings Gzip works fine, but Brotli isn't despite being officially supported now

Seems distributions will need to set EnableAcceptEncodingBrotli.

Ref docs: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html

Proposal

Feature flag off the manual compression in the lambda@edge handler. Hope we can deprecate it in future from the component inputs. Maybe leave as an option in the compat layer if there is a genuine use case for it.

[x] - Added enableHTTPCompression input.
[x] - Look into Brotli support. (A cache policy needs to be added to support Brotli. Will look into this on a separate PR).
[x] - Document enableHTTPCompression in the lambda at edge package.
[x] - Updated E2E to check for gzipped responses

@danielcondemarin danielcondemarin marked this pull request as ready for review October 21, 2020 21:02
@danielcondemarin danielcondemarin force-pushed the use-cloudfront-native-http-compression branch from a8a3952 to dd31e3d Compare October 21, 2020 21:21
@danielcondemarin danielcondemarin force-pushed the use-cloudfront-native-http-compression branch from dd31e3d to 97862bf Compare October 21, 2020 21:30
@danielcondemarin danielcondemarin changed the title Feature flag lambda at edge compression Let CloudFront do the Gzipping Oct 21, 2020
@codecov
Copy link

codecov bot commented Oct 21, 2020

Codecov Report

Merging #692 into master will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #692      +/-   ##
==========================================
+ Coverage   80.18%   80.20%   +0.02%     
==========================================
  Files          54       54              
  Lines        1731     1733       +2     
  Branches      366      369       +3     
==========================================
+ Hits         1388     1390       +2     
  Misses        285      285              
  Partials       58       58              
Impacted Files Coverage Δ
...rless-components/nextjs-component/src/component.ts 90.14% <ø> (ø)
...ayers/lambda-at-edge-compat/next-aws-cloudfront.js 100.00% <100.00%> (ø)
packages/libs/lambda-at-edge/src/api-handler.ts 86.36% <100.00%> (ø)
packages/libs/lambda-at-edge/src/build.ts 94.06% <100.00%> (+0.02%) ⬆️
...ackages/libs/lambda-at-edge/src/default-handler.ts 95.31% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef2d596...0675f1b. Read the comment docs.

@@ -46,6 +49,9 @@ describe("Pages Tests", () => {
(method) => {
it(`allows HTTP method for path ${path}: ${method}`, () => {
cy.request({ url: path, method: method }).then((response) => {
if (method !== "HEAD") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add for public files etc. but can be done later.

Cypress.Commands.add(
"verifyResponseIsCompressed",
(response: Cypress.Response) => {
expect(response.headers["content-encoding"]).to.equal("gzip");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks for adding a test in the e2e tests as well.

@dphang dphang merged commit 05fb0eb into master Oct 22, 2020
@delete-merged-branch delete-merged-branch bot deleted the use-cloudfront-native-http-compression branch October 22, 2020 00:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants