How to force a web browser NOT to cache images

后端 未结 17 650
太阳男子
太阳男子 2020-11-22 13:51

Background

I am writing and using a very simple CGI-based (Perl) content management tool for two pro-bono websites. It provides the website administrator with HTML

17条回答
  •  逝去的感伤
    2020-11-22 14:39

    Ideally, you should add a button/keybinding/menu to each webpage with an option to synchronize content.

    To do so, you would keep track of resources that may need to be synchronized, and either use xhr to probe the images with a dynamic querystring, or create an image at runtime with src using a dynamic querystring. Then use a broadcasting mechanism to notify all components of the webpages that are using the resource to update to use the resource with a dynamic querystring appended to its url.

    A naive example looks like this:

    Normally, the image is displayed and cached, but if the user pressed the button, an xhr request is sent to the resource with a time querystring appended to it; since the time can be assumed to be different on each press, it will make sure that the browser will bypass cache since it can't tell whether the resource is dynamically generated on the server side based on the query, or if it is a static resource that ignores query.

    The result is that you can avoid having all your users bombard you with resource requests all the time, but at the same time, allow a mechanism for users to update their resources if they suspect they are out of sync.

    
    
      
        
        
                
        Resource Synchronization Test
        
      
      
        
        
      
    
    

提交回复
热议问题