expires_in max-age cache control doesn't work

Deadly 提交于 2019-12-24 13:23:19

问题


I can't get the max-age cache control to work properly, I have used expires_in that resulted in a "Cache-Control:max-age=86400, public, must-revalidate" header.

But yet, the browser still sends the request to the server, at least it is defined as "304 not modified", meaning that the ETag/If-None-Match headers works properly.

I have tested it with webrick on my localhost and on heroku, with chrome 45 and Safari.

And no, my development tools are not opened, and the "disable cache" is not checked.

I also have tried to remove the ", must_revalidate: true" of the expires_in method call.

What am I missing?

Here is the output from the network in chrome: General: Remote Address:127.0.0.1:3000 Request URL:http://localtest.me:3000/api/books Request Method:GET Status Code:304 Not Modified

Response Headers: Access-Control-Allow-Origin:* Access-Control-Request-Method:* Cache-Control:max-age=86400, public, must-revalidate Connection:Keep-Alive Date:Tue, 08 Sep 2015 13:28:01 GMT Etag:W/"1f1b2d0b822830bc74e7c47a116205be" Server:WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26) X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN X-Request-Id:c70d4715-dcff-4558-85af-9d21556d406a X-Runtime:0.553353 X-Xss-Protection:1; mode=block

Request Headers: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8,fr;q=0.6,he;q=0.4 Cache-Control:max-age=0 Connection:keep-alive Host:localtest.me:3000 If-None-Match:W/"1f1b2d0b822830bc74e7c47a116205be" Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36

And here is the ruby code:

before_action :cache_control, only: [:index, :show]
def cache_control
  expires_in 1.day, public: true, must_revalidate: true
end

回答1:


Well, the headers are fine, it is just a meter of how browsers treats the reload button or shortcut keys.

I found the answer here: https://stackoverflow.com/a/16510707/789658

Here are the findings in chrome:

  1. Pressing Enter on the address bar respects the "max-age" and doesn't send a request to the server.
  2. The reload button or Cmd+R sends a request to the server with If-Modified-Since and will return 304 if resource wasn't modified since...
  3. Cmd+Shift+R sends a request to the server without even If-Modified-Since


来源:https://stackoverflow.com/questions/32460092/expires-in-max-age-cache-control-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!