在開發中禁用spro佝僂病資產緩存

[英]Disable Sprockets asset caching in development


I'm using Rails 3.2.13 and the Rails Asset Pipeline. I want to use the Asset Pipeline so I can use SASS and CoffeeScript and ERB for my assets and have the Pipeline automatically compile them, so I cannot turn off the pipeline in development. I am not precompiling assets in development ever and there is not even a public/assets/ directory.

我正在使用Rails 3.2.13和Rails資產管道。我想使用資產管道,這樣我就可以為我的資產使用SASS、coffecript和ERB,並讓管道自動編譯它們,所以我不能在開發中關閉管道。我從未在開發中預編譯過資產,甚至沒有公共/資產/目錄。

However, when I make changes to an included file, such as to a _partial.html.erb file that is included (rendered) in a layout.html.erb file, without changing the file doing the including itself (in this example layout.html.erb), Sprockets doesn't detect the change and invalidate the cache, so I keep getting the same stale file. When I'm doing this in active development, I want to disable any caching of assets so I can get the changes on every request but I cannot figure out how to do this. I have set all of the following in my development.rb:

但是,當我對包含的文件(如_partitional.html)進行更改時。在layout.html中包含(呈現)的erb文件。erb文件,在不修改包含本身的文件(在本例layout.html.erb中)的情況下,spro佝僂病不會檢測到更改並使緩存無效,所以我繼續得到相同的過期文件。在活動開發中執行此操作時,我希望禁用任何資產緩存,這樣我就可以獲得對每個請求的更改,但我無法確定如何執行此操作。在我的開發過程中,我設置了以下所有的選項。

config.action_controller.perform_caching = false
config.action_dispatch.rack_cache =  nil
config.middleware.delete Rack::Cache
config.assets.debug = true
config.assets.compress = false
config.cache_classes = false

Still, even with this, files show up under tmp/cache/assets/ and tmp/cache/sass/ and changes are not available on future requests. Right now I have to manually delete those directories every time I want to see a change.

盡管如此,在tmp/cache/assets/和tmp/cache/sass/下仍然顯示了文件,並且在以后的請求中不能進行更改。現在,每當我想看到更改時,我就必須手動刪除這些目錄。

Unfortunately, the entire contents of the How Caching Works section of the RoR Guide for the Asset Pipeline is:

不幸的是,資產管道RoR指南的緩存工作部分的全部內容是:

Sprockets uses the default Rails cache store to cache assets in development and production.

spro佝僂病使用默認的Rails緩存存儲來緩存開發和生產中的資產。

TODO: Add more about changing the default store.

TODO:添加更多關於更改默認存儲的信息。

So, how can I get Sprockets to compile assets on demand but not cache the results?

那么,如何讓spro佝僂病按需編譯資產,而不緩存結果呢?

3 个解决方案

#1


27  

Here's the magic incantation:

這是魔法咒語:

config.assets.cache_store = :null_store  # Disables the Asset cache
config.sass.cache = false  # Disable the SASS compiler cache

The asset pipeline has it's own instance of a cache and setting config.assets.cache = false does nothing, so you have to set its cache to be the null_store to disable it.

資產管道有它自己的緩存實例和設置config.assets。cache = false沒有任何作用,因此必須將其緩存設置為null_store,以禁用它。

Even then, the SASS compiler has it's own cache, and if you need to disable it, you have to disable it separately.

即使這樣,SASS編譯器也有自己的緩存,如果需要禁用它,則必須分別禁用它。

#2


1  

I created the following gist (https://gist.github.com/metaskills/9028312) that does just this and found it is the only way that works for me.

我創建了如下的gist (https://gist.github.com/metaskills/9028312),並發現這是唯一適合我的方法。

# In config/initializers/sprockets.rb

require 'sprockets'
require 'sprockets/server'

Sprockets::Server.class_eval do

  private

  def headers_with_rails_env_check(*args)
    headers_without_rails_env_check(*args).tap do |headers|
      if Rails.env.development?
        headers["Cache-Control"]  = "no-cache"
        headers.delete "Last-Modified"
        headers.delete "ETag"
      end
    end
  end
  alias_method_chain :headers, :rails_env_check

end

#3


0  

The accepted answer is not doing it correctly and it degrades the performance in development by disabling cache totally. Answering your original question, you want changes to referenced files to invalidate the asset cache even if not included directly.

公認的答案是,不能正確地執行它,它會完全禁用緩存,從而降低開發性能。回答最初的問題時,您希望對引用文件的更改使資產緩存無效,即使不直接包含這些內容。

The solution is by simply declaring such dependency such that sprockets knows that the cache should be invalidated:

解決方案是簡單地聲明這種依賴關系,使spro佝僂病知道緩存應該無效:

# layout.html.erb
<% depend_on Rails.root.join('app').join('views').join('_partial.html.erb') %>
# replace the above with the correct path, could also be relative but didn't try

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/06/06/72fe9f1c5d895cb727925c652cce9152.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com