How to make Safari send if-modified-since header?

不羁的心 提交于 2019-12-11 01:26:13

问题


When I generate a page I send headers

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Last-Modified: Mon, 04 Apr 2011 20:08:33 GMT
Vary: Accept-Encoding
Date: Mon, 11 Apr 2011 01:36:21 GMT
Content-Length: 3019

then, when I try to get this page again all browsers send correct request and get 304 answer except Safari - it never sends if-modified-since. It always reloads whole page even it havnt been changed

Does this behavior of Safari known and what to do to make Safari work in right way?


回答1:


Bart Lateur wrote a post about this, with a paragraph stating about Safari:

Safari takes this even one step further: if the header isn't a date in http standard form, then the header is simply dropped. It simply doesn't send an If- Modified-Since header on the next request.




回答2:


I ran into this with Safari 8.0. Despite providing the Last-Modified header to Safari it would not provide the If-Modified-Since header on subsequent requests. The fix in my case was to additionally set the Expires header to the same html-date as the Last-Modified header.

Here's an example of what the successful exchange looks like:

Initial Request

Standard first request from Safari.

GET http://localhost/image
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

Initial Response

I specify both the Expires and Last-Modified headers as the same valid html-date. I have not tried but I doubt Safari will honor an Expires header set to -1.

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Expires: Thu, 17 Jul 2014 19:50:58 GMT
Last-Modified: Thu, 17 Jul 2014 19:50:58 GMT
Content-Type: image/png
Content-Length: 1143902
Date: Wed, 22 Oct 2014 15:33:40 GMT

<<DATA>>

Subsequent Request

At last Safari provides the needed If-Modified-Since header.

GET http://localhost/image
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
If-Modified-Since: Thu, 17 Jul 2014 19:50:58 GMT

Subsequent Response

I can satisfyingly return a 304.

HTTP/1.1 304 Not Modified
Server: Apache-Coyote/1.1
Expires: Thu, 17 Jul 2014 19:50:58 GMT
Last-Modified: Thu, 17 Jul 2014 19:50:58 GMT
Date: Wed, 22 Oct 2014 15:33:43 GMT



回答3:


Safari is only partly free software. Other than holding your breath until Apple releases the whole of Safari as free software, as a Safari user you could use a caching proxy and configure it to break the spec and ignore the Cache-Control headers Safari sends.

Squid has the refresh_pattern directive, and I'm sure other proxies have similar functionality.

You can then configure Safari to use the proxy, or you can do it transparently in the spirit of upside-down-ternet.




回答4:


In my testing, Safari is expecting either an "Expires" or "Cache-Control" header along with "Last-Modified".

Cache-Control: max-age=0, private
Last-Modified: Thu, 17 Aug 2018 12:04:23 GMT

Or,

Expires: Thu, 17 Aug 2018 12:04:23 GMT
Last-Modified: Thu, 17 Aug 2018 12:04:23 GMT

NOTE: "max-age" was required for Safari to honor the "Last-Modified"



来源:https://stackoverflow.com/questions/5616015/how-to-make-safari-send-if-modified-since-header

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