Skip to content

Commit

Permalink
Add defaultCacheBehavior from #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hahn committed Jan 29, 2021
1 parent 8a15b08 commit 96e3158
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/MadSkills-io/fullstack-serverless/master/LICENSE)
[![npm downloads](https://img.shields.io/npm/dt/fullstack-serverless.svg?style=flat)](https://www.npmjs.com/package/fullstack-serverless)

A [serverless](http://www.serverless.com) plugin that automatically creates an AWS CloudFront distribution that serves static web content from S3 and optionally routes API traffic
to API Gateway.
A [serverless](http://www.serverless.com) plugin that automatically creates an AWS CloudFront distribution that serves static web content from S3 and optionally routes API traffic to API Gateway.

Home page - https://www.madskills.io/fullstack-serverless/

Expand All @@ -17,9 +16,10 @@ Home page - https://www.madskills.io/fullstack-serverless/
- No CORS needed
- Enables CDN caching of resources - so you don't waste Lambda invocations or API Gateway traffic
for serving static files (just [set Cache-Control headers](https://serverless.com/framework/docs/providers/aws/events/apigateway/#custom-response-headers) in API responses)
- Much more CloudWatch statistics of API usage (like bandwidth metrics)
- More CloudWatch statistics of API usage (like bandwidth metrics)
- Real world [access log](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/AccessLogs.html) - out of the box, API Gateway currently does not provide any kind of real "apache-like" access logs for your invocations
- [Web Application Firewall](https://aws.amazon.com/waf/) support - enable AWS WAF to protect your API from security threats
- Can be used to manage S3 content and CloudFront without API Gateway by simply not defining any functions

## Before you begin
* Install the serverless framework
Expand Down Expand Up @@ -502,6 +502,21 @@ Use this parameter if you want to add additional origins to the CloudFormation r

---

**defaultCacheBehavior**

_optional_, default: `not set`

```yaml
custom:
fullstack:
...
defaultCacheBehavior:
MinTTL: 3600
...
```

---

**cacheBehaviors**

_optional_, default: `not set`
Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class ServerlessFullstackPlugin {
}

processDeployment() {

if(this.cliOptions['client-deploy'] !== false) {
let region,
distributionFolder,
Expand Down Expand Up @@ -348,7 +349,7 @@ class ServerlessFullstackPlugin {
this.prepareSinglePageApp(resources.Resources);
this.prepareS3(resources.Resources);
this.prepareMinimumProtocolVersion(distributionConfig);
this.prepareCompressWebContent(distributionConfig);
this.prepareDefaultCacheBehavior(distributionConfig);

}

Expand Down Expand Up @@ -532,10 +533,15 @@ class ServerlessFullstackPlugin {
resources.WebAppS3Bucket.Properties.WebsiteConfiguration.ErrorDocument = errorDocument;
}

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

distributionConfig.DefaultCacheBehavior.Compress = compressWebContent;
distributionConfig.DefaultCacheBehavior = Object.assign({},
distributionConfig.DefaultCacheBehavior,
defaultCacheBehavior,
{ Compress: compressWebContent }
);
}

getBucketName(bucketName) {
Expand Down

0 comments on commit 96e3158

Please sign in to comment.