Code Monkey home page Code Monkey logo

non-stupid-digest-assets's Introduction

Non-stupid non-digest assets in Rails 4

What is it?

In Rails 4, there is no way to by default compile both digest and non-digest assets. This is a pain in the arse for almost everyone developing a Rails 4 app. This gem solves the problem with the minimum possible effort.

How do I install it?

Just put it in your Gemfile

gem "non-stupid-digest-assets"

If you want to whitelist non-digest assets for only certain files, you can configure a whitelist like this:

# config/initializers/non_digest_assets.rb

NonStupidDigestAssets.whitelist = [/tinymce\/.*/, "image.png"]

Be sure to give either a regex that will match the right assets or the logical path of the asset in question.

Note that the logical path is what you would provide to asset_url, so for an image at RAILS_ROOT/assets/images/foo.png the logical path is foo.png

But shouldn't I always use the Rails asset helpers anyway?

Yes. But there are some obvious cases where you can't do this:

  • Third party libraries in vendor/assets that need to include e.g. css / images
  • In a static error page, e.g. a 404 page or a 500 page
  • Referencing the assets from outside your rails application

What about other solutions?

sprockets-redirect uses a rack middleware to 302 redirect to the digest asset. This is terrible for performance because it requires 2 HTTP requests, and it also hits your ruby stack. An asset request should be handled by your webserver (e.g. nginx) because that's what it's good at.

This rake task will solve this problem, but requires an extra rake task. It won't work by default with things like capistrano / heroku. And it requires you to manage the code in your app.

Why do I need digest assets at all?

Digests are used for cache busting. Remember that if you use the non-digest assets and serve them with far-future expires headers, you will cause problems with cached assets if the contents ever need to change. You must bear this in mind when using non-digest assets.

Why is this not the default / a config option in Rails 4?

Good question. I think it should be. Complain here

non-stupid-digest-assets's People

Contributors

alexspeller avatar tvdeyen avatar aaronjensen avatar kronn avatar dnnx avatar

Watchers

 avatar

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.