admin管理员组

文章数量:1278822

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 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.

Share Improve this question asked Jan 19, 2017 at 12:20 iCyborgiCyborg 4,72815 gold badges57 silver badges86 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

I would remend 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}"

本文标签: javascriptHow to leverage browser caching in Rails 4Stack Overflow