Disable Sprockets asset caching in development on Rails 4

回眸只為那壹抹淺笑 提交于 2019-11-30 07:25:45

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.

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