Avoid actually image caching, do not just change the name of the src

0

I have an image that loads multiple times, each time with a content that will not be reused. for this problem, I found several answers suggesting something

var d = new Date();
buff.src="carrega.php?ver="+d.getTime();

Or with random number.

others suggest:

<meta Http-Equiv="Cache-Control" Content="no-cache">
<meta Http-Equiv="Pragma" Content="no-cache">
<meta Http-Equiv="Expires" Content="0">
<meta Http-Equiv="Pragma-directive: no-cache">
<meta Http-Equiv="Cache-directive: no-cache">

Well, the second solution simply does not work. And if it works it would be for the whole page and I want to disable the cache of a single image.

The first is not a solution. It returns me different images, until then ok. But every time I load, the image gets cached, which is what I want to avoid in order to not load memory usage (many images are loaded during very long usage). I also do not want to have to recommend the user to limit the cache storage space. That would not solve the problem, either.

    
asked by anonymous 28.12.2018 / 22:15

1 answer

3

One way is via .htaccess file, adding code:

<filesMatch "nome_da_imagem\.(jpg|png)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

It will avoid caching files jpg and png with a fixed name. If you only want jpg , just change the regex to:

"nome_da_imagem\.jpg$"

If you want to avoid caching any file jpg :

"\.jpg$"

Testing in Chrome using the ChromeCacheView app (CCV):

  • First I'll clear the Chrome cache and open the CCV. See it is empty:
  • I'm going to open a page that contains only a 1.jpg image and refresh the CCV window:
  • Notethatyouhavebrowsedmultiplefiles(.js,.html,etc.)butdidnotcachethe1.jpgimagethatyouownonthepage.

    NowI'mgoingtoremovethecodefrom.htaccess,cleartheChromecacheandreopenthepage.WhenupdatingtheCCVwindow,thistimetheimageappearscached(1.jpg):

        
    28.12.2018 / 23:42