AJAX - The Complete Reference

(avery) #1

272 Part II: Developing an Ajax Library^


In the case of Firefox, you don’t have to worry about the cache busting “problem”—it
simply doesn’t cache Ajax requests. But when you do know what you are doing with caching,
do you no longer have the valuable browser cache to use? If so, it would seem Ajax developers
just can’t catch a break at times.

Working with the Browser Cache


It seems that if a browser is going to cache, maybe you should take advantage of it doing so.
There are a number of headers that might be set to do this, including Cache-Control and
Expires. For example, on the server side, max-age could be set to be 31,536,000
seconds, which equals one year (60*60*24*365).

header("Cache-Control: max-age=31536000");

Or, Expires header could be set far in the future like so:

header("Expires: Mon, 05 Jun 2017 04:00:09 GMT");

This approach works in most browsers, but be careful: some browsers do not seem to respect
cache control headers when the request is made with an XHR—at least that is the case at the
time of this edition’s writing. You can test the current situation for cache control in browsers
for yourself with the example found at http://ajaxref.com/ch6/builtincache.html, which is
shown in Figure 6-20.

FIGURE 6-20 Caching and Ajax can coexist
Free download pdf