Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force browsers to refresh/download images?

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.

like image 677
uriDium Avatar asked Sep 16 '09 07:09

uriDium


People also ask

Do browsers automatically cache images?

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

What is a browser hard refresh?

A hard refresh is a way of clearing the browser's cache for a specific page, to force it to load the most recent version of a page.

How do I force Chrome to refresh on Mac?

To hard refresh on Google Chrome on Mac Hold ⇧ Shift and click the Reload button. Or, hold down ⌘ Cmd and ⇧ Shift key and then press R.


2 Answers

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.

like image 145
knittl Avatar answered Oct 13 '22 01:10

knittl


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.
like image 23
ndp Avatar answered Oct 13 '22 01:10

ndp