Cache busting via params

前端 未结 12 1159
耶瑟儿~
耶瑟儿~ 2020-11-22 03:27

We want to cache bust on production deploys, but not waste a bunch of time off the bat figuring out a system for doing so. My thought was to apply a param to the end of css

相关标签:
12条回答
  • 2020-11-22 04:04
     <script>
        var storedSrcElements = [
             "js/exampleFile.js",
             "js/sampleFile.js",
             "css/style.css"
              ];
    
        var head= document.getElementsByTagName('head')[0];
        var script;
        var link;
        var versionNumberNew = 4.6;
    
        for(i=0;i<storedSrcElements.length;i++){
         script= document.createElement('script');
         script.type= 'text/javascript';
         script.src= storedSrcElements[i] + "?" + versionNumberNew;
         head.appendChild(script);
        }     
    
    
         </script> 
    
    
           ### Change the version number  (versionNumberNew) when you want the new files to be loaded  ###
    
    0 讨论(0)
  • 2020-11-22 04:11
    <script type="text/javascript">
    // front end cache bust
    
    var cacheBust = ['js/StrUtil.js', 'js/protos.common.js', 'js/conf.js', 'bootstrap_ECP/js/init.js'];   
    for (i=0; i < cacheBust.length; i++){
         var el = document.createElement('script');
         el.src = cacheBust[i]+"?v=" + Math.random();
         document.getElementsByTagName('head')[0].appendChild(el);
    }
    </script> 
    
    0 讨论(0)
  • 2020-11-22 04:12

    Hope this should help you to inject external JS file

    <script type="text/javascript"> 
    var cachebuster = Math.round(new Date().getTime() / 1000); 
    document.write('<scr'+'ipt type="text/javascript" src="external.js?cb=' +cachebuster+'"></scr' + 'ipt>');
    </script>
    

    Source - Cachebuster code in JavaScript

    0 讨论(0)
  • In general this should be fine, but it's possible for this to not work if there is an intermediate cache (a proxy) that is configured to ignore the request parameters.

    For example, if you are serving static content through Akamai CDN, it can be configured to ignore request parameters to prevent cache busting using this method.

    0 讨论(0)
  • 2020-11-22 04:17

    Found a comparison of the 2 techniques (query string vs file name) here:

    Version as a querystring has two problems.

    First, it may not always be a browser that implements caching through which we need to bust. It is said that certain (possibly older) proxies do ignore the querystring with respect to their caching behavior.

    Second, in certain more complex deployment scenarios, where you have multiple frontend and/or multiple backend servers, an upgrade is anything but instantaneous. You need to be able to serve both the old and the new version of your assets at the same time. See for example how this affects you when using Google App Engine.

    0 讨论(0)
  • 2020-11-22 04:18

    Two questions: Will this effectively break the cache?

    Yes. Even Stack Overflow use this method, although I remember that they (with their millions of visitors per day and zillions of different client and proxy versions and configurations) have had some freak edge cases where even this was not enough to break the cache. But the general assumption is that this will work, and is a suitable method to break caching on clients.

    Will the param cause the browser to then never cache the response from that url since the param indicates that this is dynamic content?

    No. The parameter will not change the caching policy; the caching headers sent by the server still apply, and if it doesn't send any, the browser's defaults.

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