Code Monkey home page Code Monkey logo

digestion's Introduction

Digestion (Asset Pipeline Fingerprinting Controls)

The asset pipeline is a great new component of Rails 3.1. However it has a feature known as fingerprinting that makes it impossible to properly incorporate many popular JavaScript libraries (including TinyMCE, CKEditor and FancyZoom to name just a few) into the asset pipeline.

This gem patches the asset pipeline to allow these libraries to be used, by disabling the fingerprinting functionality for specific files or paths.

Installation & Usage

Add the following line to your Gemfile and run bundle install:

gem 'digestion'

The digestion gem does not change any fingerprinting options by default but allows you to set the following options in your application configuration:

# Exclude specific assets from fingerprinting (use a path, glob, regex or proc)
config.assets.digest_exclusions << "nofingerprints/*"

Rails plugins can require this gem and set these options in an initializer, making the whole process transparent to the end-developer. See the tinymce-rails project for an example of this.

The Problem With Fingerprinting

Fingerprinting is used to improve caching of assets by including a version-specific fingerprint or digest in the asset filename. From the Asset Pipeline Rails Guide:

When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change and the remote clients will request the new file. This is generally known as cachebusting.

In the Rails asset pipeline (in production mode), the MD5 hash of the file contents is inserted into the filename, e.g. application-d41d8cd98f00b204e9800998ecf8427e.js. This requires that the relevant asset_url helpers are used when referencing assets in your HTML, JavaScript or CSS (or CoffeeScript, SASS, etc).

When adding third-party code to your application, it is not always practical to make these changes. Worse still, more advanced libraries like TinyMCE load in plugins and other assets dynamically, making these changes virtually impossible. In these situations, the third-party code will work fine whilst in development but break suddenly when deployed to production.

One option is to move the assets into the public directory. Whilst this approach will work, it has some major drawbacks:

  1. It prevents you from using the other great features of the asset pipeline such as asset concatenation and minification.
  2. It leaves your assets scattered across multiple locations, making maintenance difficult.
  3. A gem that provides assets must provide an additional rake task to copy its assets into public.

Asset fingerprinting is a great feature and it is recommended you use it wherever possible. However without this gem, it is impossible to disable it for those libraries that are incompatible.

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.