Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding rails Cache-Control header on redirect

Whether I do:

head 302

or

head 307

or

redirect_to

calls in the same controller action to

response.headers['Cache-Control'] = "public, max-age=86400"

have no effect. Rails sends:

Cache-Control: no-cache

no matter what. I need to send the Cache-Control header to instruct an edge cache to serve the redirect for a day. Is this possible?

like image 892
JCSG Avatar asked Jun 03 '11 22:06

JCSG


2 Answers

You can't set Cache-Control directly into the headers (anymore?), as you need to modify the response.cache_control object (since it will be used to set the Cache-Control header later).

Luckily, the expires_in method takes care of this for you:

expires_in 1.day, :public => true

See more here: http://apidock.com/rails/ActionController/ConditionalGet/expires_in

like image 160
Jesse Rosenberger Avatar answered Sep 24 '22 18:09

Jesse Rosenberger


Try using this instead

response.headers['Cache-Control'] = 'public, max-age=300'

and make sure your in production mode. Rails wont cache in development.

like image 40
Devin M Avatar answered Sep 23 '22 18:09

Devin M