node.js Chrome 浏览器不会向服务器发送 if-modified-since 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8294034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:49:15  来源:igfitidea点击:

Chrome browser is not sending if-modified-since header to server

httpnode.jshttp-headerscache-control

提问by Asken

I have these headers being sent to the client by the server:

我将这些标头由服务器发送到客户端:

Cache-Control:private
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html
Date:Sun, 27 Nov 2011 11:10:38 GMT
ETag:"12341234"
Set-Cookie:connect.sid=e1u...7o; path=/; expires=Sun, 27 Nov 2011 11:40:38 GMT; httpOnly
Transfer-Encoding:chunked
last-modified:Sat, 26 Nov 2011 21:42:45 GMT

I want the client to validate that the file hasn't changed on the server and send a "200" if it has otherwise a "304".

我希望客户端验证文件在服务器上没有更改,如果它有“304”,则发送“200”。

Firefox sends:

Firefox 发送:

if-modified-since: Sat, 26 Nov 2011 21:42:45 GMT
if-none-match: "12341234"

Why isn't the chrome sending the same on a refresh of the page? I'm after the behavior that .Net has running:

为什么 chrome 在刷新页面时不发送相同的信息?我追求 .Net 运行的行为:

context.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate)

采纳答案by Andy McCluggage

In my experience you need more than just the "Private" Cache-Control header. You need either "Max-Age" or "Expires" to force Chrome to revalidate content with the server.

根据我的经验,您需要的不仅仅是“私有”缓存控制标头。您需要“Max-Age”或“Expires”来强制 Chrome 重新验证服务器的内容。

Remember that revalidation will only start after these time values have elapsed, so they may need to be set to a small value.

请记住,重新验证只会在这些时间值过去后开始,因此可能需要将它们设置为一个较小的值。

回答by Mark

After spending half a day on this yesterday, I tracked down what was causing the issue for me. So long as you have the Chrome object inspector/Client Debugger/Network monitor/Thing that pops up when you hit F12, Chrome will not send cache request headers. Period. (update: in newer versions of Chrome, there is a checkbox "Disable cache"). Even if you don't have the "network" tab open (ex: have the javascript console open), this checkbox still disables all cacheing.

在昨天花了半天时间之后,我找到了导致我出现问题的原因。只要您有按 F12 时弹出的 Chrome 对象检查器/客户端调试器/网络监视器/事物,Chrome 就不会发送缓存请求标头。时期。(更新:在较新版本的 Chrome 中,有一个复选框“禁用缓存”)。即使您没有打开“网络”选项卡(例如:打开 javascript 控制台),此复选框仍会禁用所有缓存。

Its sad, because debugging this from the client side obligates you to leave the network panel open to see what headers are being sent and received, and what codes are being returned. Without the network panel open, there is no way to know if your content is being cached from the client side.

很遗憾,因为从客户端调试它要求您将网络面板保持打开状态,以查看正在发送和接收哪些标头,以及正在返回哪些代码。如果没有打开网络面板,就无法知道您的内容是否正在从客户端缓存。

If you dig into your server access logs, you will notice your server returning 304s(Cached Content) the minute you close the debug window on your Chrome client. Hope this helps.

如果您深入研究服务器访问日志,您会注意到您的服务器在您关闭 Chrome 客户端上的调试窗口的那一刻返回 304s(缓存内容)。希望这可以帮助。

Chrome 24.0.1312.57

铬 24.0.1312.57

回答by Neek

I found one answer to this behaviour when using HTTPS, thought I'd share what I found. You do not specify if you are requesting via HTTP or HTTPS.

我在使用 HTTPS 时找到了这种行为的一个答案,我想我会分享我的发现。您没有指定是通过 HTTP 还是 HTTPS 请求。

"The rule is actually quite simple: any error with the certificate means the page will not be cached."

“规则实际上很简单:证书的任何错误都意味着该页面将不会被缓存。”

https://code.google.com/p/chromium/issues/detail?id=110649

https://code.google.com/p/chromium/issues/detail?id=110649

If you are using a self-signed certificate, even if you tell Chrome to add an exception for it so that the page loads, no resources from that page will be cached, and subsequent requests will not have an If-Modified-Since header.

如果您使用的是自签名证书,即使您告诉 Chrome 为其添加例外以便加载页面,也不会缓存来自该页面的任何资源,并且后续请求不会有 If-Modified-Since 标头。

回答by K-Gun

In addition (https://stackoverflow.com/a/14899869/362780):

此外(https://stackoverflow.com/a/14899869/362780):

F12 > Settings > General > Disable cache (while DevTools is open) -> uncheck this...

F12 > 设置 > 常规 > 禁用缓存(DevTools 打开时)-> 取消选中这个...

回答by Gert-Jan

Browsers have a lot of counter intuitive behavior when it comes to caching. You would expect, that if the response includes a last-modified-date, that the browser would revalidate this before reusing it. But none of the major browsers actually do that.

在缓存方面,浏览器有很多反直觉的行为。您会期望,如果响应包含最后修改日期,浏览器将在重用它之前重新验证它。但实际上没有一个主要的浏览器会这样做。

The ideal settings for your situation depend on when you want the browser to revalidate, see link below.

适合您情况的理想设置取决于您希望浏览器重新验证的时间,请参阅下面的链接。

Not only do browsers act counter intuitively, different browsers also behave differently in the same situation. For example when the user clicks on the refresh button.

不仅浏览器的行为与直觉相反,不同的浏览器在相同情况下的行为也不同。例如,当用户单击刷新按钮时。

You can read how the different browsers (Internet Explorer, Edge, Safari, FireFox, Chrome) behave with different caching directives (Etag, last-modified, must-revalidate, expires, max-age, no-cache, no-store) at https://gertjans.home.xs4all.nl/javascript/cache-control.html

您可以阅读不同的浏览器(Internet Explorer、Edge、Safari、FireFox、Chrome)如何使用不同的缓存指令(Etag、last-modified、must-revalidate、expires、max-age、no-cache、no-store)在https://gertjans.home.xs4all.nl/javascript/cache-control.html

回答by M K

I know this question is old, but still.. I noticed that chrome remembers the last refresh that you made. So if you press ctrl+shift+r (refreshing and deleting cache), and pressing ctrl+r (just refreshing), chrome keeps on deleting cache and does not show the 304 in the received response. There is a workaround for this. Press ctrl+shift+r and then go to the address bar, focus it, and hit enter. If your etags are set correctly, and your server is ready to serve a 304, you'll see a new response code in the debugger - 304. so it works.

我知道这个问题很老,但仍然......我注意到 chrome 记得你最后一次刷新。因此,如果您按 ctrl+shift+r(刷新和删除缓存),然后按 ctrl+r(只是刷新),chrome 会继续删除缓存,并且不会在收到的响应中显示 304。有一个解决方法。按 ctrl+shift+r,然后转到地址栏,将其聚焦,然后按 Enter。如果您的 etags 设置正确,并且您的服务器已准备好提供 304,您将在调试器中看到一个新的响应代码 - 304。所以它可以工作。