Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Sprockets asset caching in development on Rails 4

Another question "Disable Sprockets asset caching in development" addresses how to disable Sprockets caching in Rails 3.2. How do you do the same thing on Rails 4? I am working on a gem that is deep in the asset pipeline and having to clear tmp/cache/* and restart Rails is getting tiring.

like image 475
Cymen Avatar asked Apr 10 '14 06:04

Cymen


1 Answers

If you look at the Sprockets source, you can see that if cache_classes is true then app.assets gets set to app.assets.index, and the filesystem is no longer checked.

In order to get around this in development, you can add something similar to the following to your development.rb configuration:

# Sprockets configuration: prevent sprockets from caching assets in development
# when cache_classes is set to true
sprockets_env = nil
config.assets.configure do |env|
  sprockets_env = env

  # Sprockets environment configuration goes here
  # env.js_compressor  = :uglifier # or :closure, :yui
  # env.css_compressor = :sass   # or :yui
end

if config.cache_classes
  config.after_initialize do
    Rails.application.assets = sprockets_env
  end
end

This essentially grabs a reverence to the Sprockets::Environment object before it is overwritten by the Sprockets::Index one, and allows the filesystem to be checked for new assets even when cache_classes is true. This seems to work for us in development, so hopefully it helps someone else out as well.

like image 94
BenV Avatar answered Oct 31 '22 19:10

BenV