在 Rails 4 上的开发中禁用链轮资产缓存 [英] Disable Sprockets asset caching in development on Rails 4

查看:26
本文介绍了在 Rails 4 上的开发中禁用链轮资产缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个问题在开发中禁用链轮资产缓存"说明了如何在 Rails 3.2 中禁用链轮缓存.你如何在 Rails 4 上做同样的事情?我正在研究资产管道深处的 gem,并且必须清除 tmp/cache/* 并重新启动 Rails,这很累.

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.

推荐答案

如果你看一下 Sprockets source,你可以看到如果 cache_classes 为 true 那么 app.assets 设置为 app.assets.index,并且不再检查文件系统.

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.

为了在开发中解决这个问题,您可以添加类似于以下的内容您的 development.rb 配置:

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

这实质上是在 Sprockets::Environment 对象被 Sprockets::Index 覆盖之前获得了对它的尊重,并且即使 cache_classes 为真,也允许检查文件系统中的新资产.这似乎对我们的开发人员有用,所以希望它也能帮助其他人.

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.

这篇关于在 Rails 4 上的开发中禁用链轮资产缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆