我的 application_controller.rb 中有以下代码
before_action :set_cache_headers
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
这也会导致我的 Assets 在每次请求时重新加载。在 Rails 中,如何在每次请求时重新加载 HTML 而不是 Assets ?
我想要这样的东西(除了在 Rails 中):
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|JPG|woff)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
最佳答案
由于在 Rails for Prod env 中默认开启 action_controller 缓存,因此也设置了 Control-Cache。但是您在 ApplicationController 中的过滤器正在覆盖它,从而禁用整个缓存。 尝试将此行粘贴到您的 config/initializers/assets.rb 文件中:
Rails.application.config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{some_number}"
}
对于 some_number
,您需要一个以秒为单位的值,例如30.days.to_i
.
关于ruby-on-rails - 为 Rails Assets 设置缓存控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51540654/