Skip to content

Commit

Permalink
Enable CloudFront Compression for web assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hahn committed Apr 28, 2019
1 parent a140109 commit 2cf9bfd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ custom:
indexDocument: index.html # The index document to use
errorDocument: error.html # The error document to use
singlePageApp: false # If true 403 errors will be rerouted (missing assets) to your root index document to support single page apps like React and Angular where the js framework handles routing
compressWebContent: true # Use compression when serving web content
apiPath: api # The path prefix for your API Gateway lambdas. The path for the lambda http event trigger needs to start with this too eg. api/myMethod
clientCommand: gulp dist # Command to generate the client assets. Defaults to doing nothing
clientSrcPath: client # The path to where you want to run the clientCommand
Expand Down Expand Up @@ -301,6 +302,22 @@ If true 403 errors will be rerouted (missing assets) to your root index document

---

**compressWebContent**

_optional_, default: `true`

```yaml
custom:
fullstack:
...
compressWebContent: true
...
```

Instruct CloudFront to use compression when serving web content, see [Serving Compressed Files](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html) in the Amazon CloudFront Developer Guide.

---

**clientCommand**

_optional_, default: `not set`
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class ServerlessFullstackPlugin {
this.prepareSinglePageApp(resources.Resources);
this.prepareS3(resources.Resources);
this.prepareMinimumProtocolVersion(distributionConfig);
this.prepareCompressWebContent(distributionConfig);

}

Expand Down Expand Up @@ -449,6 +450,12 @@ class ServerlessFullstackPlugin {
resources.WebAppS3Bucket.Properties.WebsiteConfiguration.ErrorDocument = errorDocument;
}

prepareCompressWebContent(distributionConfig) {
const compressWebContent = this.getConfig('compressWebContent', true);

distributionConfig.DefaultCacheBehavior.Compress = compressWebContent;
}

getBucketName(bucketName) {
const stageBucketName = `${this.serverless.service.service}-${this.getStage()}-${bucketName}`;
return stageBucketName;
Expand Down
2 changes: 2 additions & 0 deletions lib/resources/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Resources:
- GET
- HEAD
- OPTIONS
## Compress web content: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
Compress: true
## The origin id defined above
TargetOriginId: WebApp
## Defining if and how the QueryString and Cookies are forwarded to the origin which in this case is S3
Expand Down

0 comments on commit 2cf9bfd

Please sign in to comment.