Is there a way to force browsers to refresh/download images?
Solution 1:
Append a query string with an arbitrary unique number (or time, or version number, etc.):
<img src="image.png?80172489074" alt="a cool image" />
This will result in a new request, because of the different URL.
Solution 2:
It's tough. You really want images to be cached, but then you don't want to cache them once a new ones available:
- Using expires header with a date in the past prevents any caching. Bad
- Adding a "cache busting" parameter ?342038402 can fix the problem, but can also prevent the image ever from being cached, which is not what you want. Bad.
- Using expires header with a short (lets say 1 hr) expires is better... after an hour the user will see the image, but your web server won't have to serve it every time. Compromise, but what time works? Not really feasible.
The solution? I can think of two good options:
- Look into eTags, and your ability to use them. These are designed for this situation. The browser will explicitly ask your server whether the file is up-to-date or not. You can just turn this on in apache if you don't have it aleady.
- Create a new URL for each new image (and use a far-future expires header). This is what you are working on.
Solution 3:
My favourite PHP solution:
<img src="<?php echo "image.jpg" . "?v=" . filemtime("image.jpg"); ?>" />
every change of file "create" a new version of the file