如何让 Safari 发送 if-modified-since 标头?

发布于 2024-10-31 12:04:01 字数 402 浏览 2 评论 0原文

当我生成一个页面时

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

,我会发送标头,当我尝试再次获取此页面时,所有浏览器都会发送正确的请求并获得 304 答案,除了 Safari - 它从不发送 if-modified-since。它总是重新加载整个页面,即使它没有被更改

Safari 的这种行为是否已知以及如何使 Safari 以正确的方式工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-11-07 12:04:01

Bart Lateur 写了一篇文章 关于这一点,有一段关于 Safari 的说明:

Safari 更进一步:如果标头不是 http 标准形式的日期,则标头将被简单地删除。它只是不会在下一个请求时发送 If-Modified-Since 标头。

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.

梦里泪两行 2024-11-07 12:04:01

我在 Safari 8.0 中遇到了这个问题。尽管向 Safari 提供了 Last-Modified 标头,但它不会在后续请求中提供 If-Modified-Since 标头。我的解决方案是另外将 Expires 标头设置为与 Last-Modified 标头相同的 html-date。

以下是成功交换的示例:

初始请求

来自 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

初始响应

我将 ExpiresLast-Modified 标头指定为相同的有效 html-date。我没有尝试过,但我怀疑 Safari 是否会遵守设置为 -1Expires 标头。

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>>

后续请求

最后,Safari 提供了所需的 If-Modified-Since 标头。

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

后续响应

我可以满意地返回 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

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
-小熊_ 2024-11-07 12:04:01

Safari 只是部分免费软件。除了屏住呼吸等待 Apple 将整个 Safari 作为免费软件发布之外,作为 Safari 用户,您还可以使用缓存代理并将其配置为违反规范并忽略 Safari 发送的 Cache-Control 标头。

Squid 有 refresh_pattern 指令,我确信其他代理也有类似的功能。

然后,您可以将 Safari 配置为使用代理,或者您可以本着 颠倒互联网

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.

羁拥 2024-11-07 12:04:01

在我的测试中,Safari 需要“Expires”或“Cache-Control”标头以及“Last-Modified”标头。

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

或者,

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

注意:Safari 需要“max-age”来尊重“Last-Modified”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文