HTTP caching in WCF web API seems inconsistent in browsers

I am implementing a simple REST service with the WCF Web API and am trying to set HTTP headers for caching responses.

For a simple GET like this

http://localhost:49302/my/2 

response headers are as follows:

 Server: ASP.NET Development Server/10.0.0.0 Date: Tue, 24 Jan 2012 18:18:44 GMT X-AspNet-Version: 4.0.30319 Content-Length: 233 Cache-Control: max-age=120 Vary: Accept Expires: Tue, 24 Jan 2012 18:20:44 GMT Last-Modified: Tue, 24 Jan 2012 18:18:15 GMT Content-Type: application/xml; charset=utf-8 

The client is supposed to cache the resource for two minutes.

However, using the WCF Web API test client, the behavior is incompatible in different browsers:

  • In Firefox (9.0.1), the request is cached, and first, after two minutes, a new version of the displayed resource is displayed. This behavior is as expected.
  • In Chrome (16.0.912.77 m), cache headers are not respected at all. A new version of the resource is selected for each GET request. This behavior is not expected (at least to me).
  • In Internet Explorer (9), the behavior is the same as in Chrome.

Why aren't Chrome and IE related to cache headers?

Is this a bug in the WCF web API test client?

+6
source share
2 answers

Caching is difficult. The fact that the browser can ignore cache directives certainly does not help.

According to this document, IE never cached any request with a Vary header containing nothing but Accept-Encoding and User-Agent

If I test this with a cache period of 15 seconds and I just install MaxAge and MustRevalidate, it looks like it works fine with IE9, FireFox and Chrome.

HttpResponseMessage Web API:

 result = new HttpResponseMessage<Book>(book); result.Headers.CacheControl = new CacheControlHeaderValue(); result.Headers.CacheControl.MaxAge = TimeSpan.FromSeconds(15); result.Headers.CacheControl.MustRevalidate = true; return result; 

Answer headers:

 HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Wed, 25 Jan 2012 09:13:32 GMT X-AspNet-Version: 4.0.30319 Content-Length: 98 Cache-Control: must-revalidate, max-age=15 Content-Type: application/json; charset=utf-8 Connection: Close 

I'm not sure MustRevalidate is really required, but it is recommended to use it. See Specifications here .

+4
source

The test is to replace localhost with a "real domain", so the WCF or Chrome / IE test client does not have special tricks for localhost.

+2
source

Source: https://habr.com/ru/post/906783/


All Articles