Skip to content

Commit c213cbc

Browse files
YoranBrondsemaghedamat
authored andcommitted
Configure Cache-Control and Expires headers on the uploaded files. (#53)
1 parent 0260f65 commit c213cbc

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ The client specified MUST implement functions called `getObject` and `putObject`
150150

151151
*Default:* the default S3 library is `aws-sdk`
152152

153+
### cacheControl
154+
155+
Sets the `Cache-Control` header on the uploaded files.
156+
157+
*Default:* `max-age=63072000, public`
158+
159+
### expires
160+
161+
Sets the `Expires` header on the uploaded files.
162+
163+
*Default:* `Mon Dec 31 2029 21:00:00 GMT-0300 (CLST)`
164+
153165
## Prerequisites
154166

155167
The following properties are expected to be present on the deployment `context` object:

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var minimatch = require('minimatch');
66
var DeployPluginBase = require('ember-cli-deploy-plugin');
77
var S3 = require('./lib/s3');
88

9+
var EXPIRE_IN_2030 = new Date('2030');
10+
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;
11+
912
module.exports = {
1013
name: 'ember-cli-deploy-s3',
1114

@@ -16,6 +19,8 @@ module.exports = {
1619
filePattern: '**/*.{js,css,png,gif,ico,jpg,map,xml,txt,svg,swf,eot,ttf,woff,woff2}',
1720
prefix: '',
1821
acl: 'public-read',
22+
cacheControl: 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public',
23+
expires: EXPIRE_IN_2030,
1924
distDir: function(context) {
2025
return context.distDir;
2126
},
@@ -48,6 +53,8 @@ module.exports = {
4853
var acl = this.readConfig('acl');
4954
var prefix = this.readConfig('prefix');
5055
var manifestPath = this.readConfig('manifestPath');
56+
var cacheControl = this.readConfig('cacheControl');
57+
var expires = this.readConfig('expires');
5158

5259
var filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
5360

@@ -62,7 +69,9 @@ module.exports = {
6269
prefix: prefix,
6370
bucket: bucket,
6471
acl: acl,
65-
manifestPath: manifestPath
72+
manifestPath: manifestPath,
73+
cacheControl: cacheControl,
74+
expires: expires
6675
};
6776

6877
this.log('preparing to upload to S3 bucket `' + bucket + '`', { verbose: true });

lib/s3.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ var mime = require('mime');
77
var Promise = require('ember-cli/lib/ext/promise');
88

99
var _ = require('lodash');
10-
var EXPIRE_IN_2030 = new Date('2030');
11-
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;
1210

1311
module.exports = CoreObject.extend({
1412
init: function(options) {
@@ -74,8 +72,8 @@ module.exports = CoreObject.extend({
7472
var prefix = options.prefix;
7573
var acl = options.acl;
7674
var gzippedFilePaths = options.gzippedFilePaths || [];
77-
var cacheControl = 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public';
78-
var expires = EXPIRE_IN_2030;
75+
var cacheControl = options.cacheControl;
76+
var expires = options.expires;
7977

8078
var manifestPath = options.manifestPath;
8179
var pathsToUpload = filePaths;

tests/unit/index-nodetest.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('s3 plugin', function() {
120120
return previous;
121121
}, []);
122122

123-
assert.equal(messages.length, 2);
123+
assert.equal(messages.length, 4);
124124
});
125125

126126
describe('required config', function() {
@@ -170,18 +170,24 @@ describe('s3 plugin', function() {
170170
it('adds default config to the config object', function() {
171171
delete context.config.s3.filePattern;
172172
delete context.config.s3.prefix;
173+
delete context.config.s3.cacheControl;
174+
delete context.config.s3.expires;
173175

174176
assert.isUndefined(context.config.s3.filePattern);
175177
assert.isUndefined(context.config.s3.prefix);
178+
assert.isUndefined(context.config.s3.cacheControl);
179+
assert.isUndefined(context.config.s3.expires);
176180

177181
var plugin = subject.createDeployPlugin({
178182
name: 's3'
179183
});
180184
plugin.beforeHook(context);
181185
plugin.configure(context);
182186

183-
assert.isDefined(context.config.s3.filePattern);
184-
assert.isDefined(context.config.s3.prefix);
187+
assert.equal(context.config.s3.filePattern, '**/*.{js,css,png,gif,ico,jpg,map,xml,txt,svg,swf,eot,ttf,woff,woff2}');
188+
assert.equal(context.config.s3.prefix, '');
189+
assert.equal(context.config.s3.cacheControl, 'max-age=63072000, public');
190+
assert.equal(new Date(context.config.s3.expires).getTime(), new Date('Tue, 01 Jan 2030 00:00:00 GMT').getTime());;
185191
});
186192
});
187193

tests/unit/lib/s3-nodetest.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ describe('s3', function() {
9797
cwd: process.cwd() + '/tests/fixtures/dist',
9898
prefix: 'js-app',
9999
acl: 'public-read',
100-
bucket: 'some-bucket'
100+
bucket: 'some-bucket',
101+
cacheControl: 'max-age=1234, public',
102+
expires: '2010'
101103
};
102104

103105
var promises = subject.upload(options);
@@ -109,9 +111,9 @@ describe('s3', function() {
109111
assert.equal(s3Params.Body.toString(), 'body: {}\n');
110112
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
111113
assert.equal(s3Params.Key, 'js-app/app.css');
112-
assert.equal(s3Params.CacheControl, 'max-age=63072000, public');
114+
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
115+
assert.equal(s3Params.Expires, '2010');
113116
assert.isUndefined(s3Params.ContentEncoding);
114-
assert.deepEqual(s3Params.Expires, new Date('2030'));
115117
});
116118
});
117119

@@ -128,7 +130,9 @@ describe('s3', function() {
128130
cwd: process.cwd() + '/tests/fixtures/dist',
129131
prefix: 'js-app',
130132
acl: 'public-read',
131-
bucket: 'some-bucket'
133+
bucket: 'some-bucket',
134+
cacheControl: 'max-age=1234, public',
135+
expires: '2010'
132136
};
133137

134138
var promises = subject.upload(options);
@@ -137,9 +141,9 @@ describe('s3', function() {
137141
.then(function() {
138142
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
139143
assert.equal(s3Params.Key, 'js-app/app.css.gz');
140-
assert.equal(s3Params.CacheControl, 'max-age=63072000, public');
141144
assert.equal(s3Params.ContentEncoding, 'gzip');
142-
assert.deepEqual(s3Params.Expires, new Date('2030'));
145+
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
146+
assert.equal(s3Params.Expires, '2010');
143147
});
144148
});
145149
});

0 commit comments

Comments
 (0)