Disable Sprockets asset caching in development

喜你入骨 提交于 2019-11-29 01:25:21

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.

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

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.

# 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

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:

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