Skip to content

Commit 3660e4b

Browse files
committed
Merge pull request #63 from LevelbossMike/cache-control
Allow cache-control headers to be set by options
2 parents d0993c9 + 48e84b0 commit 3660e4b

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ The ACL to apply to the objects.
105105

106106
*Default:* `'public-read'`
107107

108+
### cacheControl
109+
110+
Sets the `Cache-Control` header on uploaded files.
111+
112+
*Default:* `'max-age=0, no-cache'`
113+
108114
### distDir
109115

110116
The root directory where the file matching `filePattern` will be searched for. By default, this option will use the `distDir` property of the deployment context.

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
filePattern: 'index.html',
1717
prefix: '',
1818
acl: 'public-read',
19+
cacheControl: 'max-age=0, no-cache',
1920
distDir: function(context) {
2021
return context.distDir;
2122
},
@@ -38,6 +39,7 @@ module.exports = {
3839
var bucket = this.readConfig('bucket');
3940
var prefix = this.readConfig('prefix');
4041
var acl = this.readConfig('acl');
42+
var cacheControl = this.readConfig('cacheControl');
4143
var revisionKey = this.readConfig('revisionKey');
4244
var distDir = this.readConfig('distDir');
4345
var filePattern = this.readConfig('filePattern');
@@ -49,6 +51,7 @@ module.exports = {
4951
bucket: bucket,
5052
prefix: prefix,
5153
acl: acl,
54+
cacheControl: cacheControl,
5255
filePattern: filePattern,
5356
filePath: filePath,
5457
revisionKey: revisionKey,

lib/s3.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ module.exports = CoreObject.extend({
3939
var plugin = this._plugin;
4040
var bucket = options.bucket;
4141
var acl = options.acl;
42+
var cacheControl = options.cacheControl;
4243
var allowOverwrite = options.allowOverwrite;
43-
var key = options.filePattern + ":" + options.revisionKey;
44-
var revisionKey = joinUriSegments(options.prefix, key);
44+
var key = options.filePattern + ":" + options.revisionKey;
45+
var revisionKey = joinUriSegments(options.prefix, key);
4546
var putObject = Promise.denodeify(client.putObject.bind(client));
4647
var gzippedFilePaths = options.gzippedFilePaths || [];
4748
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
@@ -51,7 +52,7 @@ module.exports = CoreObject.extend({
5152
Key: revisionKey,
5253
ACL: acl,
5354
ContentType: mime.lookup(options.filePath) || 'text/html',
54-
CacheControl: 'max-age=0, no-cache'
55+
CacheControl: cacheControl
5556
};
5657

5758
if (isGzipped) {

tests/unit/index-nodetest.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('s3-index plugin', function() {
2020
var DEFAULT_PREFIX = '';
2121
var DEFAULT_FILE_PATTERN = 'index.html';
2222
var DEFAULT_ACL = 'public-read';
23+
var DEFAULT_CACHE_CONTROL = 'max-age=0, no-cache';
2324

2425
function s3Stub(returnValue) {
2526
return function(options) {
@@ -110,6 +111,31 @@ describe('s3-index plugin', function() {
110111
.then(function() {
111112
var expected = {
112113
acl: DEFAULT_ACL,
114+
cacheControl: DEFAULT_CACHE_CONTROL,
115+
bucket: BUCKET,
116+
prefix: DEFAULT_PREFIX,
117+
filePattern: DEFAULT_FILE_PATTERN,
118+
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
119+
gzippedFilePaths: [],
120+
revisionKey: REVISION_KEY,
121+
allowOverwrite: false
122+
};
123+
124+
assert.deepEqual(s3Options, expected);
125+
});
126+
});
127+
128+
it('passes cacheControl options based on the cacheControl option to the s3-abstraction', function() {
129+
var cacheControl = 'max-age=3600';
130+
context.config['s3-index'].cacheControl = cacheControl;
131+
132+
var promise = plugin.upload(context);
133+
134+
return assert.isFulfilled(promise)
135+
.then(function() {
136+
var expected = {
137+
acl: DEFAULT_ACL,
138+
cacheControl: cacheControl,
113139
bucket: BUCKET,
114140
prefix: DEFAULT_PREFIX,
115141
filePattern: DEFAULT_FILE_PATTERN,
@@ -132,6 +158,7 @@ describe('s3-index plugin', function() {
132158
.then(function() {
133159
var expected = {
134160
acl: DEFAULT_ACL,
161+
cacheControl: DEFAULT_CACHE_CONTROL,
135162
bucket: BUCKET,
136163
prefix: DEFAULT_PREFIX,
137164
filePattern: DEFAULT_FILE_PATTERN,

tests/unit/lib/s3-nodetest.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('s3', function() {
7272
bucket: bucket,
7373
prefix: '',
7474
acl: 'public-read',
75+
cacheControl: 'max-age=0, no-cache',
7576
gzippedFilePaths: [],
7677
filePattern: filePattern,
7778
revisionKey: revisionKey,
@@ -175,6 +176,20 @@ describe('s3', function() {
175176
});
176177
});
177178

179+
it('allows `cacheControl` option to be passed to customize the used cache-control', function() {
180+
var cacheControl = 'max-age=3600';
181+
182+
options.cacheControl = cacheControl;
183+
184+
var promise = subject.upload(options);
185+
186+
return assert.isFulfilled(promise)
187+
.then(function() {
188+
assert.equal(s3Params.CacheControl, cacheControl, 'cache-control passed correctly');
189+
190+
});
191+
});
192+
178193
describe("when revisionKey was already uploaded", function() {
179194
beforeEach(function() {
180195
options.revisionKey = "123";

0 commit comments

Comments
 (0)