3

I am caching images on user's browser to avoid repeated network requests. However, sometimes those images will be deleted from the server and fresh ones will take their place with the same name.

I want to serve those new images to the user but don't know how to tell browser to do so. It's not sending any request (Obviously, that's what were cached used for).

Google results say it can't be done. Is that so?

2 Answers 2

3

If the file is cached in the browser, it won't be requested. So you can't send any directive to refresh it. But you can:

  • change the file name
  • add a version number imag.jpg?v=5
  • use a shorter expiration if your images are going to change often

I personnaly use the second option. Define a version number in your application config file. You can then change the version number when you change your design, and all images / css / js will refresh.

define('VERSION_NUMBER', '2.1')

Then use the define in your views:

<img src="image.jpg?v=<?php echo VERSION_NUMBER;?>" />
Sign up to request clarification or add additional context in comments.

5 Comments

Editing file name or adding version number will require too much modification in the system and I was hoping to use it as last option. Thanks for the help though.
@AkshatGoel i edited my answer. Once configured, you can switch to a new version by changing only one constant.
Actually, these are not images that we (developers) control. These are user managed images and each user has tons of such images. Any of those may get deleted and replaced with fresh ones These are not static images too. These are generated via script using PHP and content-type tag in header.
@AkshatGoel do you keep track of the images in a database? If so use the image ID as version number. Or use the timestamp of the latest upload. Or add a version number in database.
Yes. That was my last option. I am going for this method now. I think there is no other option.
0

There are plenty of sophisticated ways of controlling caching with HTTP. The general mechanism is that you tell the browser to re-check the image after a specified expiry date. Rather than requesting the whole image, the browser will send headers such as If-Modified-Since, and the server can either respond 304 Not Modified or send the new image.

Exactly how you do this will depend on how you're serving the images. If you're just using Apache to serve them from the file system, look into the mod_expires module.

If you search for "HTTP caching", you should find plenty of different techniques. The one thing you can't do is actively tell the browser to uncache something when it's modified on the server, as you can only respond to requests that the browser makes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.