2.3. Cache Control

Whenever you are dealing with repeated calls to the same page, you should be concerned about browser caching. For those unaware, web browsers tend to cache certain resources to improve the speed with which sites are downloaded and displayed. This can result in a tremendous speed increase on frequently visited web sites, but can also cause problems for pages that change frequently. If you are making several Ajax calls, you need to be aware that caching may cause you problems.

The best way to deal with caching is to include a header with caching information on any data being sent from the server to the browser. This can be done using the Cache-Control and Expires headers, which should be set up as follows:

Cache-Control: no-cache
Expires: Fri, 30 Oct 1998 14:19:41 GMT

This tells the browser not to cache the data coming from the specific URL. Instead, the browser always calls a new version from the server instead of a saved version from its own cache. Most browsers support the Cache-Control header, and almost all support the Expires header (which is set to a date in the past to prevent caching). Using a combination of the two headers ensures that all browsers will not cache the page.

NOTE

It is important to note that technically the no-cache directive simply requires the browser to ensure that the resource in question is the most up-to-date version available. If the version in the cache is the most recent version, then the cached version is used. To force the browser ...

Get Professional Ajax, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.