Giter VIP home page Giter VIP logo

asset_hash's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.