Rail Override Cache-Control Header When Redirecting

Am I doing:

head 302 

or

 head 307 

or

 redirect_to 

calls the value in the same controller action

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

do not work. Rails sends:

 Cache-Control: no-cache 

no matter what. I need to send a Cache-Control header to indicate the border cache for day redirection. Is it possible?

+4
source share
4 answers

You cannot set Cache-Control directly in the headers (anymore?), Since you need to change the response.cache_control object (since it will be used to set the Cache-Control header later).

Fortunately, the expires_in method will take care of this for you:

 expires_in 1.day, :public => true 

More details here: http://apidock.com/rails/ActionController/ConditionalGet/expires_in

+11
source

Try using this instead

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

and make sure you are in production mode. Rails will not be cached during development.

+1
source

With Rails 5 you can do

 response.cache_control = 'public, max-age=86400' 
+1
source
 . I need to send the Cache-Control header to instruct an edge cache to serve the redirect for a day. 

How is this possible? in the case of temporary redirection, browsers will always try to get the original url first, and when redirecting, they will try a different URL, which, if caching on proxy servers can be submitted from there. But the browser will again maintain first contact with your server.

0
source

All Articles