Adding Expires Headers In Ruby On Rails

Whenever a first-time visitor visits your page, it may take several HTTP requests before the web page finish loading. To speed up your website, it helps to use Expires header to make any web page components cachaeble. By doing this, you can avoid any unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images but it is still highly recommended that they also be used on ALL components such scripts, stylesheets, and Flash as well.

ScreenHunter 1276 Jan. 02 13.19 Adding Expires Headers In Ruby On Rails

So here’s a nice short snippet from Mohit Jain that lets you add expires headers to assets in Ruby on Rails:

config.static_cache_control = "public, max-age=31536000"  # in production.rb file     

And in your config.ru file:

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run YourApplicationName::Application

Actually, there are many ways to do this but this one in particular can be very helpful, especially in cases where you want to add expiry headers to assets hosted on Heroku.

(Image credit for parking meter: HerCampus.com)

Incoming search terms for the article:

Related Posts

Active Admin For Ruby On Rails

Creating a Psychedelic Art Effect in Your Portraits

Inspirational Photo Retouches By Cristian Girotto

Create Your Own Sticker Design Via Photoshop