在Rails 4中禁用Sprockets资产缓存的开发模式

8

另一个问题 "在开发中禁用Sprockets资产缓存" 解决了如何在Rails 3.2中禁用Sprockets缓存。在Rails 4上怎么做相同的事情?我正在开发一个深入资产管道的gem,不得不清除tmp/cache/*并重启Rails令人疲倦。


对于Rails 5/6,请参见https://stackoverflow.com/questions/42954065/i-am-not-able-to-disable-the-sprockets-assets-caching-in-rails - Udi
1个回答

5
如果你查看Sprockets的源代码,你会发现如果cache_classes为true,则app.assets将被设置为app.assets.index,此时不再检查文件系统。
为了在开发中解决这个问题,你可以在development.rb配置中添加类似于以下内容的内容。
# 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

这实际上是在Sprockets :: Index对象覆盖之前获取对Sprockets :: Environment对象的引用,并允许在cache_classes为true时检查新资产的文件系统。这在开发中对我们有效,希望它也能帮助其他人。

1
啊,我们目前仍在使用Rails 4.0,所以当我们进行升级时,我们需要重新调查。感谢您的提醒。 - BenV
对于Rails 5/6,请参见https://stackoverflow.com/questions/42954065/i-am-not-able-to-disable-the-sprockets-assets-caching-in-rails - Udi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接