Prevent Browser Caching of Dynamic Content | 2 | WebReference

Prevent Browser Caching of Dynamic Content | 2


Prevent Browser Caching of Dynamic Content [con't]

The easiest way to include request headers in your pages that you never want cached is to use META tags. These provide information about the HTML document and are typically used to specify page description, keywords, author of the document, last modified, and other metadata. Metadata is not displayed on the page, but is machine parsable:

The first request header field in the example above is the Pragma field. It's used to include implementation-specific directives that might apply to any recipient along the request/response chain. All pragma directives specify optional behavior from the viewpoint of the protocol. It is the replacement for the Control-Cache in HTTP 1.1 and accepts one directive of no-cache. The next request header field is the Cache-Control header. It is used to specify directives that must be obeyed by all caching mechanisms along the request/response chain. The directives specify behavior intended to prevent caches from adversely interfering with the request or response. Hence, these directives typically override the default caching algorithms. It is by far the most common header for caching and accepts many directives (for a complete list, there are a few sources in the references section at the end of this article). The Cache-Control header above combines five of the most common directives and tokens, including the no-cache directive, in order to maintain backwards compatibility with the HTTP 1.0 protocol.

The Expires header field gives the date and time after which the response is considered stale. A stale cache entry may not normally be returned by a cache unless it is first validated with the origin server. The format is an absolute date and time as defined by the HTTP-date standard, such as: Expires: Thu, 01 Dec 1994 16:00:00 GMT. Interestingly, HTTP/1.1 clients must treat other invalid date formats, especially including the value "0", as in the past (I.E., "already expired").

The Last-Modified and If-Modified-Since dates will be evaluated against the server's in order to determine whether or not to refetch the document. The strategy here is to create a new Date with a parameter of zero. By doing so, the JavaScript engine will create a date using Unix time, or POSIX time, which is the number of seconds which have elapsed since midnight, January first, 1970. Using a date so far in the past all but guarantees that the server's will be newer.

Server Solutions

Another approach is to tackle the problem from the server-side. Here, the goal is to modify the response headers. Just as the request contains one or more request headers, the server reply contains at least one response header. Typical ones include the Content-Type of the resource, the Content-Length of the reply, and the Server name and version information. Here are the response headers that were supplied by my Abyss testing server. All I had to do was add the following line to the Ajax.Request's options hash:

Figure 1

How you set the response headers depends heavily on what language you are working in, but all Web languages include some variation of setResponseHeader(), including setHeader(), or even just header(). While it would be highly impractical to demonstrate how to set the response headers in every language out there, we can look at a few popular ones, to give you a foundation as to what headers to include and how one would go about setting them.

Here is some Java servlet code setting the relevant fields:

Here is some PHP, embedded in the page:

Finally, here are two ways to disable browser caching using ASP.NET:

Using code:

By including the following declaration in your page:

Here is what came back when I switched the weatherService with an ASP.NET page and included the C# snippet above:

Figure 2

Aside from the Cache-Control, Pragma, and Expires cache-related lines, you'll notice that there is one extra header called Test. As you can see, it is possible to create any header you want using the Request.AppendHeader() method.

The ability to manipulate request and response headers opens up a world of possibilities. Once you can prevent browsers from caching your dynamic page content, it's only a stone's throw to controlling the circumstances of when and for how long page elements are cached. ASP.NET's Cache object is a class that supports advanced caching mechanisms. Caching can be done on both the client and server and can be dictated based on a variety of factors, including, query string parameters, form controls, and even the browser type. As the Web becomes increasingly dynamic, efficient caching strategy will become more integral to the performance and success of websites.


Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob specializes in Monte Carlo simulations for determining optimal investment strategies and Web application development. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects, Monte Carlo spreadsheet generation, and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: May 11, 2009