5

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?

2
  • Why are you serving cache control on a redirect? Commented Jun 4, 2011 at 4:36
  • Because the redirect is always to the same location, but that location is stored in a database and I don't want my rails app to have to look it up on every request. I want cloudfront to do the redirect without having to go to rails first. Commented Jun 5, 2011 at 20:56

4 Answers 4

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

Mega thanks on this. You can stick this inside your action, or put it in a before_filter like I did. Just by itself.
Unfortunately this still didn't solve the problem for me, though I suspect it's because I'm in development mode. Uber stupid system
1

With Rails 5 you can do

response.cache_control = 'public, max-age=86400'

Comments

0
. 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 case of temp redirect , browsers will always try to get original url first and on redirect they will try other url,which if cached on proxies can be served from there. But again browser will still make first contact with your server.

1 Comment

I want cloudfront to serve the redirect once it has received it from the origin for a URI. The documentation says it will do this if the Cache-Control header is set in the redirecting response from the origin.
0

Try using this instead

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

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

2 Comments

Why would lowering the max age make any difference at all?
All apps in heroku run in production mode, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.