forked from smysnk/gulp-cloudfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joshua Bellamy-Henn
committed
Mar 14, 2014
1 parent
528e967
commit 5377dbb
Showing
3 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,58 @@ | ||
gulp-cloudfront | ||
=============== | ||
# [gulp](https://github.com/wearefractal/gulp)-cloudfront [![Build Status](https://travis-ci.org/smysnk/gulp-cloudfront.png?branch=master)](https://travis-ci.org/smysnk/gulp-cloudfront) | ||
|
||
> Updates the [Default Root Object](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DefaultRootObject.html) of a [CloudFront](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html) distribution | ||
## Purpose | ||
|
||
Content distribution networks like [CloudFront](http://aws.amazon.com/cloudfront/) let you cache static assets in [Edge Locations](http://aws.amazon.com/about-aws/globalinfrastructure/) for extended periods of time. | ||
A problem occurs however when you go to release a new version of your website, previous visitors of your website will hit their cache instead. | ||
In the case of CloudFront, you will need to invalidate items or wait for the cache TTL to expire before vistors of your website will see the vew version. | ||
|
||
A solution to this problem is adding a revisioned number to the name your static assets. The gulp plugin [gulp-rev-all](https://github.com/smysnk/gulp-rev-all) can assist in this process. eg. unicorn.css => unicorn-098f6bcd.css | ||
You can then use [gulp-s3](https://github.com/nkostelnik/gulp-s3)* to upload the revisioned files to a S3 bucket which CloudFront points to. | ||
|
||
**Finally gulp-cloudfront comes in during the final step, to update a CloudFronts' distribution Default Root Object to the latest revisioned index.html.** | ||
Updating the Default Root Object only takes 5-10 minutes and all new visitors to your website will no longer see the old cached content. | ||
|
||
* Note: I have submitted a [pull request](https://github.com/nkostelnik/gulp-s3/pull/7) to gulp-s3 as it currently does not support file contents from streams, which makes it incompatible with [gulp-gzip](https://github.com/jstuckey/gulp-gzip). In the mean time you can use my forked version [here](https://github.com/smysnk/gulp-s3). | ||
|
||
## Install | ||
|
||
Install with [npm](https://npmjs.org/package/gulp-rev-all) | ||
|
||
``` | ||
npm install --save-dev gulp-cloudfront | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
var gulp = require('gulp'); | ||
var s3 = require("gulp-s3"); | ||
var revall = require('gulp-rev-all'); | ||
var gzip = require("gulp-gzip"); | ||
var cloudfront = require("gulp-cloudfront"); | ||
|
||
var options = { gzippedOnly: true }; | ||
var aws = { | ||
"key": "AKIAI3Z7CUAFHG53DMJA", | ||
"secret": "acYxWRu5RRa6CwzQuhdXEfTpbQA+1XQJ7Z1bGTCx", | ||
"bucket": "dev.example.com", | ||
"region": "eu-west-1", | ||
"distributionId": "E1SYAKGEMSK3OD" | ||
}; | ||
|
||
gulp.task('default', function () { | ||
gulp.src('dist/**') | ||
.pipe(revall()) | ||
.pipe(cloudfront(aws)) | ||
.pipe(gzip()) | ||
.pipe(s3(aws, options)); | ||
|
||
}); | ||
``` | ||
|
||
|
||
## License | ||
|
||
[MIT](http://opensource.org/licenses/MIT) © [Joshua Bellamy-Henn](http://www.psidox.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
var es = require('event-stream'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var gutil = require('gulp-util'); | ||
var AWS = require('aws-sdk'); | ||
var map = require('map-stream'); | ||
var Q = require('q'); | ||
var through = require('through2'); | ||
|
||
module.exports = function(options) { | ||
|
||
options = options || {}; | ||
var cloudfront = new AWS.CloudFront({ | ||
accessKeyId: options.key, | ||
secretAccessKey: options.secret | ||
}); | ||
|
||
var updateDefaultRootObject = function (defaultRootObject) { | ||
|
||
var deferred = Q.defer(); | ||
|
||
// Get the existing distribution id | ||
cloudfront.getDistribution({ Id: options.distributionId }, function(err, data) { | ||
|
||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
|
||
// AWS Service returns errors if we don't fix these | ||
data.DistributionConfig.Comment = ''; | ||
data.DistributionConfig.Logging.Bucket = ''; | ||
data.DistributionConfig.Logging.Prefix = ''; | ||
data.DistributionConfig.DefaultRootObject = defaultRootObject; | ||
|
||
// Update the distribution with the new default root object | ||
cloudfront.updateDistribution({ | ||
IfMatch: data.ETag, | ||
Id: options.distributionId, | ||
DistributionConfig: data.DistributionConfig | ||
}, function(err, data) { | ||
|
||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
deferred.resolve(); | ||
} | ||
|
||
}); | ||
|
||
} | ||
}); | ||
|
||
return deferred.promise; | ||
|
||
}; | ||
|
||
return through.obj(function (file, enc, callback) { | ||
|
||
var self = this; | ||
|
||
if (file.isNull()) { | ||
return callback(null, file); | ||
} else if (file.isStream()) { | ||
throw new Error('Streams are not supported!'); | ||
} | ||
|
||
// Update the default root object once we've found the index.html file | ||
if (file.path.match(/index\-[a-f0-9]{8}\.html/gi)) { | ||
|
||
updateDefaultRootObject(path.basename(file.path)) | ||
.then(function() { | ||
return callback(null, file); | ||
}, function(err) { | ||
self.emit('error', new gutil.PluginError('gulp-cloudfront', err)); | ||
callback(null, file); | ||
|
||
}) | ||
|
||
} else { | ||
return callback(null, file); | ||
} | ||
|
||
}); | ||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "gulp-cloudfront", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"dependencies": { | ||
"gulp-util": "~2.2.14", | ||
"event-stream": "~3.1.0", | ||
"through2": "~0.4.0", | ||
"chalk": "~0.4.0", | ||
"aws-sdk": "~2.0.0-rc11", | ||
"q": "~1.0.1", | ||
"map-stream": "~0.1.0" | ||
}, | ||
"devDependencies": { | ||
"gulp": "~3.5.5", | ||
"mocha": "~1.13.0", | ||
"should": "*" | ||
}, | ||
"scripts": { | ||
"test": "cd test/ && mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/smysnk/gulp-cloudfront.git" | ||
}, | ||
"keywords": [ | ||
"gulpplugin", | ||
"aws", | ||
"cloudfront", | ||
"cdn" | ||
], | ||
"homepage": "https://github.com/smysnk/gulp-cloudfront", | ||
"author": "Joshua Bellamy-Henn <smysnk@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/smysnk/gulp-rev-all/issues" | ||
}, | ||
"engines": { | ||
"node": ">=0.8" | ||
} | ||
} |