Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a web browser to cache Images

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?

like image 282
The Unknown Avatar asked Aug 16 '09 21:08

The Unknown


People also ask

Do browsers automatically cache images?

Yes the browser will cache them, often even when your headers say otherwise.

Can images be cached?

Image caching essentially means downloading an image to the local storage in the app's cache directory (or any other directory that is accessible to the app) and loading it from local storage next time the image loads.


1 Answers

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.

like image 178
Paul Dixon Avatar answered Oct 02 '22 19:10

Paul Dixon