How to force a web browser to cache Images

41.5k views Asked by At

I am writing a small application which serves images from the local computer, so they can be accessed as http://localhost:12345/something/something (which returns a jpeg).

How can I force the browser to cache this, so only a single request would be sent to the server. Would this header be sufficient

HTTP/1.1 200 OK
Cache-Control: public, max-age=99936000
Content-Length: 123456
Content-Type: image/jpeg

This seems to work with Firefox 3.x but would it be sufficient globally for other browser as well?

3

There are 3 answers

0
Niko On

try the expires http header: http://en.wikipedia.org/wiki/List_of_HTTP_headers

You probably have to merge this with your cache-control

3
annakata On

You can't and nor should you - this is the user's decision to make not yours.

What you can do is strongly suggest with the right headers, but even then you should expect t deal with a number of HEAD requests ever after.

0
Paul Dixon On

A Last-Modified and Expires header might also be useful additions.

Your server should also check for requests featuring an If-Modified-Since header, and return a 304 Not Modified response if possible to speed things along.