tags:

views:

231

answers:

5

I have a problem where users are reporting that their images aren't being uploaded and the old ones are still there. On closer inspection the new images are there, they just have the same name as the old one. What I do on the upload is that I rename the images for SEO purposes. When they delete an image the old index becomes available and is reused. Therefore it has the same image name.

Is there a way to (i thought maybe there might be a meta tag for this) to tell the browser to not use its cahce?

The better answer is to rename the image to something totally new. I will get working on that but in the mean time is the quick solution while I work on the bigger problem.

+8  A: 

append a querystring with a random 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 filename URL

knittl
It must be rather unique than random.
Gumbo
+1, but strictly speaking it's not different filename ;-) The filename component is the same, but the URL is certainly different.
Michael Krelin - hacker
Gubmo, unique is a bit too much, the best in this case would be to add file's mtime — this way it will only be reloaded when needed.
Michael Krelin - hacker
The best way will be to use a version number of the release.
rahul
phoenix, why is it better? You'll have to keep releasing on every image update then;-)
Michael Krelin - hacker
@hacker: Randomness doesn’t guarantee that two generated values may not be identical. But uniqueness does guarantee that two generated values are not identical. And isn’t the modification date of that file a unique datum to that file?
Gumbo
Gumbo, you can, of course, use hash over file contents, but basically, you want the file to be reloaded whenever it is modified and this is when the time stamp is updated in case of normal workflow. You *don't* want the file to be unconditionally reloaded on each request. Besides, you don't want it to be unique to file — the uniqueness is provided by the uniqueness of filename+query string combination.
Michael Krelin - hacker
Good enough for now. Random will do fine too. I only plan to have this in for a couple of hours while i rewrite the logic to not reuse identifiers therefore creating an unique URL every time. Thanks everyone.
uriDium
MTime is a great idea, keep up the good work hacker
Salvin Francis
A: 

you can control the cache behaviour by playing with the HTTP headers.

setting the expires header in past would force browser to not use cached version.

Expires: Sat, 26 Jul 1997 05:00:00 GMT

You can consult the RFC to have more details.

RageZ
RageZ the problem here is that you will also have to get certain browser's developers consult the RFC.
Michael Krelin - hacker
yes ... but at least that the relevant document. after implementation glitch is kind of ...
RageZ
I'm all for proper implementation, but we have to deal with reality.
Michael Krelin - hacker
Most of browsers implement that correctly, back to the IE6 days that was ugly but now should be fine.
RageZ
IE6 days are not over...
Michael Krelin - hacker
+3  A: 

You can put http-equiv in <meta> tag which will tell browser not to use cache (or even better -- use it in some defined way), but it is better to configure server to send proper http cache headers. Check out article on caching.

Still, some browsers might not support all of http standard, but I believe it's the way to go.

samuil
That's the way to go if you plan on doing it by the book.
Mike
On the contrary, using workarounds supports browsers' bad support for these ideas ;) I see your point, and I know further discussion usually leads nowhere - something is "better" but does not work as intended, something is "worse" but do work. It is all up to webdev, what will he/she choose.
samuil
samuil, actually, *whom* (s)he wants for an audience ;-)
Michael Krelin - hacker
+1  A: 

If you look at the data that is exchanged between your browser and the server, you'll see that the browser will send a HTTP HEAD request for the images. The result will contain the modification time (but not the actual image data). Make sure that this time changes if the image changes on the server and the browser should download the image again.

Aaron Digulla
A: 

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.
ndp