Skip to content

Commit

Permalink
Merge pull request #15 from axelhzf/add-support-to-shared-cache
Browse files Browse the repository at this point in the history
Add support to shared cache
  • Loading branch information
jussi-kalliokoski committed Mar 8, 2017
2 parents ae89770 + ac95294 commit a4042d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This is a function that takes an options object as its argument, and the options
* `routes` A key-value pair of the routes and their options.
* `cache` (optional) Override values for default cache options:
- `cacheTime` (defaults to `null`) a value in seconds to use for cache headers. If `null`, no cache headers are applied.
- `sharedCacheTime` (default to `null`) a value in seconds to use for shared cache headers (`s-maxage`). `s-maxage` directive overrides both the `max-age` and `expires` header, and most well behaved CDNs will obey it.
- `public` (defaults to `true`) a boolean value on whether to include the `public` directive in the `Cache-Control` header. If false, `private` directive is used instead.
- `allowTransform` (defaults to `false`) a boolean value on whether to allow transforms of the cached content. If `false`, the `no-transform` directive is applied to the `Cache-Control` header.
- `useExpires` (defaults to `false`) if specified, applies the `Expires` header as well. Use with caution as the cache will expire after the `cacheTime` has passed of the publish time.
Expand All @@ -52,8 +53,10 @@ gulp.task("publish", function () {
key: "$&",
// use gzip for assets that benefit from it
gzip: true,
// cache static assets for 20 years
cacheTime: 630720000
// cache static assets for 1 week for user
cacheTime: 604800,
// cache static assets for 20 years on the CDN
sharedCacheTime: 630720000
},

"^assets/.+$": {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/applyCacheHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module.exports = function (file, route) {
var directives = [];
directives.push("max-age=" + route.cacheTime);

if (typeof route.sharedCacheTime === "number") {
directives.push("s-maxage=" + route.sharedCacheTime);
}

if ( !route.allowTransform ) {
directives.push("no-transform");
}
Expand Down
18 changes: 18 additions & 0 deletions test/awspublishRouterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ describe("awspublishRouter", function () {
Math.abs(new Date(file.s3.headers.Expires) - Date.now()).should.be.below(350 * 1000);
}));

it("should allow enabling the shared cache directive in the header", createSimpleTest({
cache: {
allowTransform: true
},

routes: {
"^.+$": {
key: "$&",
cacheTime: 300,
sharedCacheTime: 500
}
}
}, function (file) {
var directives = file.s3.headers["Cache-Control"].split(", ");
directives.should.contain("max-age=300");
directives.should.contain("s-maxage=500");
}));

it("should allow enabling Expires header in the cache options", createSimpleTest({
cache: {
useExpires: true
Expand Down

0 comments on commit a4042d8

Please sign in to comment.