简体   繁体   English

Internet Explorer/Firefox 中的硬刷新和 XMLHttpRequest 缓存

[英]Hard refresh and XMLHttpRequest caching in Internet Explorer/Firefox

I make an Ajax request in which I set the response cacheability and last modified headers:我发出了一个 Ajax 请求,在该请求中设置了响应可缓存性和最后修改的标头:

if (!String.IsNullOrEmpty(HttpContext.Current.Request.Headers["If-Modified-Since"]))
{
    HttpContext.Current.Response.StatusCode = 304;
    HttpContext.Current.Response.StatusDescription = "Not Modified";
    return null;
}
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetLastModified(DateTime.UtcNow);

This works as expected.这按预期工作。 The first time I make the Ajax request, I get 200 OK .我第一次提出 Ajax 请求时,我得到200 OK The second time I get 304 Not Modified .我第二次得到304 Not Modified

When I hard refresh in Chrome (Ctrl+F5), I get 200 OK - fantastic!当我在 Chrome (Ctrl+F5) 中硬刷新时,我得到200 OK - 太棒了!

When I hard refresh in Internet Explorer/Firefox, I get 304 Not Modified .当我在 Internet Explorer/Firefox 中硬刷新时,我得到304 Not Modified However, every other resource (JS/CSS/HTML/PNG) returns 200 OK .但是,所有其他资源 (JS/CSS/HTML/PNG) 都会返回200 OK

The reason is because the "If-Not-Modified" header is sent for XMLHttpRequest's regardless of hard refresh in those browsers.原因是“如果未修改”header 是针对 XMLHttpRequest 发送的,无论这些浏览器中的硬刷新如何。 I believe Steve Souders documents it here .我相信 Steve Souders在这里记录了它。

I have tried setting an ETag and conditioning on "If-None-Match" to no avail (it was mentioned in the comments on Steve Souders page).我尝试设置一个 ETag 并在“If-None-Match”上进行调节,但无济于事(Steve Souders 页面的评论中提到了这一点)。

Has anyone got any gems of wisdom here?有没有人在这里得到任何智慧的宝石?

Thanks, Ben谢谢,本

Update更新

I could check the "If-Modified-Since" against a stored last modified date.我可以对照存储的最后修改日期检查“If-Modified-Since”。 However, hopefully this question will help other SO users who find the header to be set incorrectly.但是,希望这个问题能帮助其他发现 header 设置不正确的 SO 用户。

Update 2更新 2

Whilst the request is sent with the "If-Modified-Since" header each time.虽然每次都使用“If-Modified-Since”header 发送请求。 Internet Explorer won't even make the request if an expiry isn't set or is set to a future date.如果到期未设置或设置为未来日期,Internet Explorer 甚至不会发出请求。 Useless!无用!

Update 3更新 3

This might as well be a live blog now.这还不如现在是一个实时博客。 Internet Explorer doesn't bother making the second request when localhost. Internet Explorer 不会在 localhost 时发出第二个请求。 Using a real IP or the loopback will work.使用真正的 IP 或环回将起作用。

Prior to IE10, IE does not apply the Refresh Flags (see http://blogs.msdn.com/b/ieinternals/archive/2010/07/08/technical-information-about-conditional-http-requests-and-the-refresh-button.aspx ) to requests that are not made as a part of loading of the document.在 IE10 之前,IE 不应用刷新标志(请参阅http://blogs.msdn.com/b/ieinternals/archive/2010/07/08/technical-information-about-conditional-http-requests-and-the -refresh-button.aspx ) 对不是作为加载文档的一部分进行的请求。

If you want, you can adjust the target URL to contain a nonce to prevent the cached copy from satisfying a future request.如果需要,您可以调整目标 URL 以包含随机数,以防止缓存副本满足未来的请求。 Alternatively, you can send max-age=0 to force IE to conditionally revalidate the resource before each reuse.或者,您可以发送 max-age=0 以强制 IE 在每次重用之前有条件地重新验证资源。

As for why the browser reuses a cached resource that didn't specify a lifetime, please see http://blogs.msdn.com/b/ie/archive/2010/07/14/caching-improvements-in-internet-explorer-9.aspx至于为什么浏览器会复用一个没有指定生命周期的缓存资源,请看http://blogs.msdn.com/b/ie/archive/2010/07/14/caching-improvements-in-internet-explorer -9.aspx

The solution i came upon for consistent control was managing the cache headers for all request types.我为实现一致控制而遇到的解决方案是管理所有请求类型的缓存标头。

So, I forced standard requests the same as XMLHttpRequests, which was telling IE to use the following cache policy: Cache-Control: private, max-age=0.因此,我强制标准请求与 XMLHttpRequests 相同,它告诉 IE 使用以下缓存策略:Cache-Control: private, max-age=0。

For some reason, IE was not honoring headers for various requests types.出于某种原因,IE 不尊重各种请求类型的标头。 For example, my cache policy for standard requests defaulted to the browser and for XMLHttpRequests, it was set to the aforementioned control policy.例如,我对标准请求的缓存策略默认为浏览器,而对于 XMLHttpRequests,它被设置为上述控制策略。 However, making a request to something like /url as a standard get request, render the result properly.但是,将 /url 之类的请求作为标准获取请求,可以正确呈现结果。 Unfortunately, making the same request to /url as an XMLHttpRequest, would not even hit the server because the get request was cached and the XMLHttpRequest was hitting the same url.不幸的是,向 /url 发出与 XMLHttpRequest 相同的请求,甚至不会到达服务器,因为获取请求已被缓存并且 XMLHttpRequest 正在访问相同的 Z572D4E421E5E6B9BC11D815E8A027112。

So, either force your cache policy on all fronts or make sure you're using different access points (uri's) for your request types.因此,要么在所有方面强制您的缓存策略,要么确保您对请求类型使用不同的访问点 (uri)。 My solution was the former.我的解决方案是前者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM