Skip to content

Asset hasher for Cloudfront custom origin serving of static assets

Notifications You must be signed in to change notification settings

donaldpiret/asset_hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AssetHash

About

AssetHash is a gem that allows you to easily generate unique paths for your static assets in order to avoid caching issues.
Its main purpose is to allow you to use Amazon Cloudfront with a custom origin policy to serve your static assets easily.

It uses an MD5 hash of your static asset to modify the filename, so only changed files will result in a different filename.

It also comes with a rake task to automatically create hashed versions of your asset filenames, useful for deployments.

Usage

Add the gem to your Gemfile

gem "asset_hash"

Set up a CloudFront distribution with a custom origin that points to your website’s URL.

There is a rake task included in the gem that guides you through creating CloudFront distribution easily, to run it just the following command from your app directory.

rake asset:hash:create_cloudfront_distribution

It is an interactive rake task that will ask you for your Amazon credentials and the domain you want to point the custom origin to.

Alternatively you can check the following website as well as the Amazon CloudFront documentation for help on setting up a custom origin CloudFront distribution:
http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?DistributionConfigDatatype.html#CustomOriginChildElements

Configure your environment file to use this CloudFront distribution as your asset host and the hashed filenames in the helpers.

config.action_controller.asset_host = Proc.new do |source, request|
  request.ssl? ? "https://distributionID.cloudfront.net" : "http://distributionID.cloudfront.net"
end
config.action_controller.asset_path = Proc.new do |source|
  AssetHash.fingerprint(source)
end

Set up your deployment file (Capistrano, Chef callback or other) to run the included rake task on deploy

rake asset:hash:generate

This will copy all your assets from a path like stylesheets/application.css to something like stylesheets/applications-234fe3b632356brdedf3115ee743250.css and create a gzipped version as well at stylesheets/applications-234fe3b632356brdedf3115ee743250.css.gz
This ensures that when CloudFront caches your static assets, it will always serve the up-to-date version.

The code by default plays nice with Jammit by including the assets directory to the default images, javascripts and stylesheets directories, and you can customize which directories are scanned for assets using AssetHash::Base.asset_paths = ['custom', 'directories']

License

This project rocks and uses MIT-LICENSE.

About

Asset hasher for Cloudfront custom origin serving of static assets

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages