Search code examples
phpwordpresscaching

No cache header whatever


I have a webcam that uploads a image every 15 min to a folder on my webserver. And then i have a wordpress page that display this image.

The problem is the browser caches the image and it will not update when i refresh the page after 15 min.

I have tryed different options like function wp_get_nocache_headers(), img.decoding = "async";, and some META no-cashe stuff, but nothing works.

There is a 99% chance im doing it wrong!

Can anyone giv my a step, by step guide how to prevent browser cache on my webcam image? And only the one page with the image.


Solution

  • Let's suppose you have a path to the image, such as

    my/path/to/the/image.jpg
    

    and when the browser loads this, then it caches it, that is, it saves it locally so the image is not requested for again until the cache lifecycle passes. And of course you have no control over the cache of your users' browser.

    But, if you have something like this instead:

    my/path/to/the/image.jpg?v=123
    

    then whoever loaded this image without the v parameter being 123 will have his/her browser actually retrieving the image. So, you can associate a timestamp to the upload of your image and append it as a parameter, like

    t=<the timestamp>
    

    where "the timestamp" is of course a number and you change that number whenever you override the image, so you allow the browser of your users to cache the image, but at the next upload, the timestamp being different from the one that was previously used will actually force the browser of your user to retrieve the image and not to rely on the cache when the image is to be overriden.