【发布时间】:2017-01-19 12:20:29
【问题描述】:
我正在使用 GTMetrix 查看我的网站速度,它正在向我展示这一点(查看下图)。
如何利用浏览器缓存来加快 Rails 4 中的网站加载速度?
为了延迟解析JS,我已经放了
<%= javascript_include_tag 'application' %>
在 /html 标记之前。
【问题讨论】:
标签: javascript ruby-on-rails ruby-on-rails-4
我正在使用 GTMetrix 查看我的网站速度,它正在向我展示这一点(查看下图)。
如何利用浏览器缓存来加快 Rails 4 中的网站加载速度?
为了延迟解析JS,我已经放了
<%= javascript_include_tag 'application' %>
在 /html 标记之前。
【问题讨论】:
标签: javascript ruby-on-rails ruby-on-rails-4
我建议使用单独的 Web 服务器,例如 NGINX 来为 .js 和 .css 文件设置缓存头,从而消除从 Rails 提供静态文件的麻烦。
如果您真的想使用纯 Rails (app/web) 服务器,解决方案是将这段代码放入 config/environments/production.rb
铁路 5
config.public_file_server.headers = {
'Cache-Control' => "public, s-maxage=#{365.days.to_i}, maxage=#{180.days.to_i}",
'Expires' => "#{1.year.from_now.to_formatted_s(:rfc822)}"
}
铁路 4
config.static_cache_control = "public, s-maxage=#{365.days.to_i}, maxage=#{180.days.to_i}"
【讨论】:
config.static_cache_control 设置为config/environments/production.rb
'Cache-Control' => "public, s-maxage=#{365.days.to_i}, maxage=#{180.days.to_i}"