Refresh image with a new one at the same url

后端 未结 19 3273
南旧
南旧 2020-11-21 22:00

I am accessing a link on my site that will provide a new image each time it is accessed.

The issue I am running into is that if I try to load the image in the backgr

相关标签:
19条回答
  • 2020-11-21 22:57

    As an alternative to...

    newImage.src = "http://localhost/image.jpg?" + new Date().getTime();
    

    ...it seems that...

    newImage.src = "http://localhost/image.jpg#" + new Date().getTime();
    

    ...is sufficient to fool the browser cache without bypassing any upstream caches, assuming you returned the correct Cache-Control headers. Although you can use...

    Cache-Control: no-cache, must-revalidate
    

    ...you lose the benefits of the If-Modified-Since or If-None-Match headers, so something like...

    Cache-Control: max-age=0, must-revalidate
    

    ...should prevent the browser from re-downloading the entire image if it hasn't actually changed. Tested and working on IE, Firefox, and Chrome. Annoyingly it fails on Safari unless you use...

    Cache-Control: no-store
    

    ...although this still may be preferable to filling upstream caches with hundreds of identical images, particularly when they're running on your own server. ;-)

    Update (2014-09-28): Nowadays it looks like Cache-Control: no-store is needed for Chrome as well.

    0 讨论(0)
提交回复
热议问题