how to clear or replace a cached image

后端 未结 20 1616
囚心锁ツ
囚心锁ツ 2020-11-29 02:04

I know there are many ways to prevent image caching (such as via META tags), as well as a few nice tricks to ensure that the current version of an image is shown with every

20条回答
  •  借酒劲吻你
    2020-11-29 02:17

    It sounds like the base of your question is how to get the old version of the image out of the cache. I've had success just making a new call and specifying in the header not to pull from cache. You're just throwing this away once you fetch it, but the browser's cache should have the updated image at that point.

        var headers = new Headers()
        headers.append('pragma', 'no-cache')
        headers.append('cache-control', 'no-cache')
    
        var init = {
          method: 'GET',
          headers: headers,
          mode: 'no-cors',
          cache: 'no-cache',
        }
    
        fetch(new Request('path/to.file'), init)
    

    However, it's important to recognize that this only affects the browser this is called from. If you want a new version of the file for any browser once the image is replaced, that will need to be accomplished via server configuration.

提交回复
热议问题