Ruby on Rails: how to set content expiration for 15 minutes if it is an AJAX request
Is the following a good form of detecting AJAX request and setting expiration for 15 minutes so that the same GET will not require any network traffic?
# in controller
if request.xhr?
response.headers['Expires'] = (Time.now + 15.minutes).httpdate
response.headers['Cache-Control'] = '' # override any no-cache, must-revalidate
end
Update: I found a shorter alternative, which is expires_in
# in controller
if request.xhr?
expires_in(15.minutes)
en开发者_JAVA百科d
although, after that, the header becomes:
Cache-Control: max-age=900, private
which used to be
Expires: Tue, 13 Jul 2010 00:59:47 GMT
when it was the earlier version. But note that Cache-Control
is for HTTP/1.1 and even a couple years ago, it was supported by 99% of the browsers, as mentioned in the High Performance Website book.
Ok, since there is no answer yet, I am going to use the one in the update above:
if request.xhr?
expires_in(15.minutes)
end
I tried it and it works well.
精彩评论