CGI Programming OpenFAQ


How can I stop the browser from caching the pages my script creates?

Contributors:

You can't force a browser to do anything.

The WWW has a carefully-engineered design for influencing cacheing, of documents of all kinds, not only for HTML.

The relevant headers are intended to influence the behaviour not only of browsers, but also of cache proxy servers that may be involved. Cache proxies are widely used - and by some of the largest service providers, and really must be taken into account in any serious consideration of this issue.

There is an excellent Tutorial by Mark Nottingham, which I strongly recommend to anyone considering this topic; also there is some fine information at the Vancouver-webpages site by Andrew Daviel which shows the benefits of handling cacheing properly, and how to prevent it when necessary without doing too much harm.

In many practical situations, it is crude and quite unnecessary to stop the cacheing entirely; if for example a document is being updated at predictable intervals, it is much better to set it to expire at around the time that the next update is due, rather than to inhibit cacheing entirely. As so often, authors would be advised to work with the web, rather than trying to fight against it.

That should suffice: but as there is so much misleading "information" about, it might be useful to make a few specific points to motivate people to visit the tutorial instead of continuing with some widely-propagated cargo-cult solutions - that appear to work in some simple cases, but fail in many real WWW situations.

The use of META HTTP-EQUIV is the most widespread example of cargo cult: it won't have the slightest effect for the majority of cache proxies, because most web servers do not check the HTML they are sending and generate real HTTP headers on the basis of these METAs, and cache proxies typically don't analyze the HTML that they are cacheing to find out whether it might have some HTTP headers hidden inside it. Since we are discussing CGI scripts, which have absolutely no problem with generating any valid HTTP headers that they wish, there really is no point in considering this hack with META HTTP-EQUIV, and, anyway, what would you do with your other media (images etc.) with that approach? Real HTTP headers can be sent with all of the media you may be generating from a CGI script: there is no need to consider HTML as special in this regard, the mechanisms that are engineered into HTTP work harmoniously for all media.

There is also a widespread belief that the HTTP header Pragma: no-cache is a command from a server to a client (browser) to not cache the document. This is not really correct according to the specification (although some browsers do honour it in that sense): the specified purpose of this header is as a request from the client (through the proxy caches) to the server, used for example when the user hits their "Reload" button.

Another source of confusion seems to be the rather widespread belief that adjusting the headers (or METAs for that matter) of a new version of a document could have any effect on a previously-cached old version of a document. The fact is that neither the client (browser) nor the proxy cache server will consult the web server until they are good and ready, unless the user commands them to do so (by means of a reload button or other browser option); and when they do consult the web server, they will find the new version of the document: no additional effort is needed in order to achieve this. Keep in mind that the headers that are sent out with a particular version of a document relate only to the cacheability of that particular version, and can have no influence on any other versions of that document.


Previous | Next | Table of Contents