iCyborg
iCyborg

Reputation: 4728

How to leverage browser caching in Rails 4?

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.

enter image description here

Upvotes: 9

Views: 2828

Answers (1)

ErvalhouS
ErvalhouS

Reputation: 4216

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

Upvotes: 14

Related Questions