Easy deployment of your assets on CloudFront or S3. When enabled in production, your assets will be served from CloudFront/S3 which will result in a speedier front-end.
Hosting your assets on CloudFront ensures minimum latency for all your visitors. But deploying your assets requires some additional management that this gem provides.
The best way to expire your assets on CloudFront is to upload the asset to a new unique url. The gem will calculate the MD5-hash of the asset and incorporate that into the URL.
By using the MD5-hash we can easily determine which assets aren't uploaded yet. This speeds up the deployment considerably.
gem install cloudfront_asset_host
Add the gem to your
The gem relies on the
gzip utility. Make sure it is available locally.
Make sure your s3-credentials are stored in config/s3.yml like this:
access_key_id: 'access_key' secret_access_key: 'secret'
or with environment specified:
production: access_key_id: 'access_key' secret_access_key: 'secret'
Create an initializer to configure the plugin config/initializers/cloudfront_asset_host.rb
The cname option also accepts a
String with the
%d parameter (e.g. "assets%d.example.com" for multiple hosts).
CloudfrontAssetHost::Uploader.upload!(:verbose => true, :dryrun => false) before your deployment. Put it for example in your Rakefile or Capistrano-recipe. Verbose output will include information about which keys are being uploaded. Enabling dryrun will skip the actual upload if you're just interested to see what will be uploaded.
If the plugin is enabled. Rails' internal
asset_id functionality will be overridden to point to the location of the assets on Cloudfront.
When using in combination with SASS and/or asset_packager it is recommended to generate the css files and package your assets before uploading them to Cloudfront. For example, call
- Add support for CNAME-interpolation [wpeterson]
- Add ability to specify an exclude regex for CDN content [wpeterson]
- Configure directories to upload through CloudfrontAssetHost.asset_dirs [wpeterson]
- CloudfrontAssetHost.cname accepts Proc [foresth]
- Rewrite all css-files when some images are modified [foresth]
- Enable S3-logging with CloudfrontAssetHost.s3_loggin (defaults to false) [foresth]
- Ability to insert prefix to paths to uncompressed files [foresth]
- Ability to define environment specific credentials in s3.yml [foresth]
Fixes and improvements:
- Strip query-parameters from image-urls in CSS [wpeterson]
- Use Digest::MD5 as md5-implementation [wpeterson, mattdb, foresth]
- Fix bug working with paths with spaces [caleb]
- Fix bug serving gzipped-assets to IE
- First release
Feel free to fork the project and send pull requests.
Thanks to these people who contributed patches:
Tested on Rails 2.3.5 with SASS and AssetPackager plugins
Created at Wakoopa
Copyright (c) 2010 Menno van der Sman, released under the MIT license