Dajbych.net


Internet Explorer 9 and HTTP cache

, 4 minutes to read

http logo

If a web page provides the date of the last modification of the content in the HTTP header, the browser can use a conditional HTTP request. In the case of a request for a page that is already stored in the browser cache, its age is sent with the request. The server then sends the content of the page only if it has changed in the meantime. Otherwise, it will only send a header indicating that the change has not been made, saving time and network traffic.

The ninth version of Internet Explorer brings improvements to this behavior. However, an essential part is the implementation by the dynamic web application, which should provide at least the last time of the content change. Historically, a static page has a date of modification of the file – including its contents. In the case of dynamic, however, this date has nothing to do with its content.

Settings from ASP.NET

Simply put, ASP.NET engine processes a server script that generates HTML code. It is then stored in the server-side cache so that it is enough to process the script only once in a short time for many identical requests from different clients. The HTML code is then also stored in the client-side cache so that the server only needs to be queried once for several identical requests in quick succession. And along the way, the HTML page can still be stored on a proxy server. Influencing where a page can and cannot be saved is done via method SetCacheability:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

Almost every article on the web contains a publication time, but few provide this time in a form readable for caching – i.e. in the HTTP response header. A single line of code is enough:

HttpContext.Current.Response.Cache.SetLastModified(article.Modified);

The SetLastModified method takes as its only argument the time of the last modification of a dynamically generated page of the DateTime type. Other systems will then take care of everything else. The browser sends a request with the If-Modified-Since header, and IIS and ASP.NET respond accordingly.

Expires / max-age

The server can define the time of the page’s validity by sending a max-age header, the value of which expresses the number of seconds for which the content of the page is valid. The second option is to send a header Expires, the value of which expresses the expiration time. After the expiration date, the browser must ask the server whether it is still valid. It uses the If-Modified-Since header, in which it states the time of the last page load. The server can answer this with HTTP/304 Not Modified if it finds that no change has occurred since then. The browser then uses the page from the cache and saves the transfer of the entire page.

ETag

An ETag is a hash of the page’s content, which is automatically generated by IIS. The browser then accompanies the second query for the same URL with the If-None-Match header, in which it lists the ETag obtained after the first request. The server can then answer HTTP/304 Not Modified.

Vary

It may happen that the returned content depends on an HTTP header, typically Accept-Language. The server can say that the browser can only use a cached page if the headers listed in the Vary header value are identical. Internet Explorer 9 ignores the values of Accept-Encoding and User-Agent of the Vary header, because the cached content is always decompressed and the content of the page should not be dependent on the browser used.

Redirect

Internet Explorer 9 now automatically caches pages that only redirect. With the 301 Moved Permanently redirect, IE 9 behaves in such a way that the page is cached unless this is forbidden (Cache-Control: no-cache). In the case of redirects 302 Found and 307 Temporary Redirect, it is only cached if this is permitted (Cache-Control: max-age=120).

Back/forward

The behavior when clicking on the back or forward buttons has also been modified. Internet Explorer 9 behaves according to the RFC2616 standard, which states that if the user clicks on the buttons mentioned above, the browser should display the cached page even if it has expired. In this case, the value of must-revalidate in the heading cache-control is also disregarded. However, a page that forbids it is never cached (Cache-Control: no-cache). However, if you want to cache a page only for use in the case of backward or forward navigation, just use Cache-Control: max-age=0.

Unless the server says otherwise

Many servers do not specify when the page expires at all. In this situation, IE9 behaves, unlike its predecessors, according to RFC2616. If the server sends at least the Last-Modified header, the page validity period is calculated as follows:

max-age = (DownloadTime - LastModified) * 0.1

If the server does not even bother to send the Last-Modified header, the page is considered valid for twelve hours or until the browser is closed, whichever comes first.