Skip to content

Commit 9a56924

Browse files
committed
Merge pull request #42 from taylon/gzip-index
Fix #27: Conditionally add ContentEncoding:gzip header when index.html is gzipped
2 parents f24a709 + 6680c9e commit 9a56924

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module.exports = {
2929
s3DeployClient: function(/* context */) {
3030
return new S3({ plugin: this });
3131
},
32+
gzippedFiles: function(context) {
33+
return context.gzippedFiles || [];
34+
},
3235
allowOverwrite: false
3336
},
3437

@@ -41,6 +44,7 @@ module.exports = {
4144
var revisionKey = this.readConfig('revisionKey');
4245
var distDir = this.readConfig('distDir');
4346
var filePattern = this.readConfig('filePattern');
47+
var gzippedFiles = this.readConfig('gzippedFiles');
4448
var allowOverwrite = this.readConfig('allowOverwrite');
4549
var filePath = path.join(distDir, filePattern);
4650

@@ -51,6 +55,7 @@ module.exports = {
5155
filePattern: filePattern,
5256
filePath: filePath,
5357
revisionKey: revisionKey,
58+
gzippedFilePaths: gzippedFiles,
5459
allowOverwrite: allowOverwrite
5560
};
5661

lib/s3.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ module.exports = CoreObject.extend({
3636
},
3737

3838
upload: function(options) {
39-
var client = this._client;
40-
var plugin = this._plugin;
41-
var bucket = options.bucket;
42-
var acl = options.acl;
43-
var allowOverwrite = options.allowOverwrite;
44-
var key = path.join(options.prefix, options.filePattern + ":" + options.revisionKey);
45-
var putObject = Promise.denodeify(client.putObject.bind(client));
39+
var client = this._client;
40+
var plugin = this._plugin;
41+
var bucket = options.bucket;
42+
var acl = options.acl;
43+
var allowOverwrite = options.allowOverwrite;
44+
var key = path.join(options.prefix, options.filePattern + ":" + options.revisionKey);
45+
var putObject = Promise.denodeify(client.putObject.bind(client));
46+
var gzippedFilePaths = options.gzippedFilePaths || [];
47+
var isGzipped = gzippedFilePaths.indexOf('index.html') !== -1;
4648

4749
var params = {
4850
Bucket: bucket,
@@ -52,6 +54,10 @@ module.exports = CoreObject.extend({
5254
CacheControl: 'max-age=0, no-cache'
5355
}
5456

57+
if (isGzipped) {
58+
params.ContentEncoding = 'gzip';
59+
}
60+
5561
return this.fetchRevisions(options)
5662
.then(function(revisions) {
5763
var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey);

tests/unit/lib/s3-nodetest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('s3', function() {
7373
bucket: bucket,
7474
prefix: '',
7575
acl: 'public-read',
76+
gzippedFilePaths: [],
7677
filePattern: filePattern,
7778
revisionKey: revisionKey,
7879
filePath: 'tests/unit/fixtures/test.html',
@@ -137,6 +138,16 @@ describe('s3', function() {
137138
});
138139
});
139140

141+
it('sets the Content-Encoding header to gzip when the index file is gziped', function() {
142+
options.gzippedFilePaths = ['index.html'];
143+
var promise = subject.upload(options);
144+
145+
return assert.isFulfilled(promise)
146+
.then(function() {
147+
assert.equal(s3Params.ContentEncoding, 'gzip', 'contentEncoding is set to gzip');
148+
});
149+
});
150+
140151
it('allows `prefix` option to be passed to customize upload-path', function() {
141152
var prefix = 'my-app';
142153

0 commit comments

Comments
 (0)