Disable Sprockets asset caching in development on Rails 4

谁说胖子不能爱 提交于 2019-11-29 09:31:42

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22979839/disable-sprockets-asset-caching-in-development-on-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!