Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Cache-Control and Expires headers on the uploaded files. #53

Merged
merged 4 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ The client specified MUST implement functions called `getObject` and `putObject`

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

### cacheControl

Sets the `Cache-Control` header on the uploaded files.

*Default:* `max-age=63072000, public`

### expires

Sets the `Expires` header on the uploaded files.

*Default:* `Mon Dec 31 2029 21:00:00 GMT-0300 (CLST)`

## Prerequisites

The following properties are expected to be present on the deployment `context` object:
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var minimatch = require('minimatch');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var S3 = require('./lib/s3');

var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;

module.exports = {
name: 'ember-cli-deploy-s3',

Expand All @@ -16,6 +19,8 @@ module.exports = {
filePattern: '**/*.{js,css,png,gif,ico,jpg,map,xml,txt,svg,swf,eot,ttf,woff,woff2}',
prefix: '',
acl: 'public-read',
cacheControl: 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public',
expires: EXPIRE_IN_2030,
distDir: function(context) {
return context.distDir;
},
Expand Down Expand Up @@ -48,6 +53,8 @@ module.exports = {
var acl = this.readConfig('acl');
var prefix = this.readConfig('prefix');
var manifestPath = this.readConfig('manifestPath');
var cacheControl = this.readConfig('cacheControl');
var expires = this.readConfig('expires');

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

Expand All @@ -62,7 +69,9 @@ module.exports = {
prefix: prefix,
bucket: bucket,
acl: acl,
manifestPath: manifestPath
manifestPath: manifestPath,
cacheControl: cacheControl,
expires: expires
};

this.log('preparing to upload to S3 bucket `' + bucket + '`', { verbose: true });
Expand Down
6 changes: 2 additions & 4 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ var mime = require('mime');
var Promise = require('ember-cli/lib/ext/promise');

var _ = require('lodash');
var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;

module.exports = CoreObject.extend({
init: function(options) {
Expand Down Expand Up @@ -74,8 +72,8 @@ module.exports = CoreObject.extend({
var prefix = options.prefix;
var acl = options.acl;
var gzippedFilePaths = options.gzippedFilePaths || [];
var cacheControl = 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public';
var expires = EXPIRE_IN_2030;
var cacheControl = options.cacheControl;
var expires = options.expires;

var manifestPath = options.manifestPath;
var pathsToUpload = filePaths;
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('s3 plugin', function() {
return previous;
}, []);

assert.equal(messages.length, 2);
assert.equal(messages.length, 4);
});

describe('required config', function() {
Expand Down Expand Up @@ -170,18 +170,24 @@ describe('s3 plugin', function() {
it('adds default config to the config object', function() {
delete context.config.s3.filePattern;
delete context.config.s3.prefix;
delete context.config.s3.cacheControl;
delete context.config.s3.expires;

assert.isUndefined(context.config.s3.filePattern);
assert.isUndefined(context.config.s3.prefix);
assert.isUndefined(context.config.s3.cacheControl);
assert.isUndefined(context.config.s3.expires);

var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
plugin.configure(context);

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

Expand Down
16 changes: 10 additions & 6 deletions tests/unit/lib/s3-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ describe('s3', function() {
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket'
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};

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

Expand All @@ -128,7 +130,9 @@ describe('s3', function() {
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket'
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};

var promises = subject.upload(options);
Expand All @@ -137,9 +141,9 @@ describe('s3', function() {
.then(function() {
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
assert.equal(s3Params.Key, 'js-app/app.css.gz');
assert.equal(s3Params.CacheControl, 'max-age=63072000, public');
assert.equal(s3Params.ContentEncoding, 'gzip');
assert.deepEqual(s3Params.Expires, new Date('2030'));
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
assert.equal(s3Params.Expires, '2010');
});
});
});
Expand Down