如何在Rails 4中利用浏览器缓存? [英] How to leverage browser caching in Rails 4?

查看:44
本文介绍了如何在Rails 4中利用浏览器缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GTMetrix来查看我的网站速度,并且正在向我显示此速度(请参见下图).

如何利用浏览器缓存来加快Rails 4中的网站加载速度?

为了推迟解析JS,我已经放了

 <%= javascript_include_tag'application'%> 

/html标记之前.

解决方案

我建议使用单独的Web服务器,例如NGINX来设置.js和.css文件的缓存头,从而消除了从Rails提供静态文件的麻烦./p>

如果您真的想使用纯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}" 

I am using GTMetrix to see my site speed and it is showing me this (check below image).

How can I Leverage browser caching to speed up the site loading speed in Rails 4?

To defer parsing JS, I have already put

<%= javascript_include_tag 'application' %>

before /html tag.

解决方案

I would recommend using separate web server, like NGINX to set cache headers for .js and .css files, removing the hassle of serving static files from Rails.

If you really wanna go with a pure Rails (app/web)server, the solution is putting this piece of code in config/environments/production.rb

RAILS 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)}"
}

RAILS 4

config.static_cache_control = "public, s-maxage=#{365.days.to_i}, maxage=#{180.days.to_i}"

这篇关于如何在Rails 4中利用浏览器缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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