简体   繁体   English

使用requirejs缓存问题js文件

[英]Caching issue js files using requirejs

I keep having issues with the cached js files, which causes the user to not have the latest js and the browser to give js errors on files it can't find that should be included with requirejs. 我一直在缓存的js文件中遇到问题,这导致用户没有最新的js,并且浏览器在文件上出现js错误,而文件中找不到js错误。

My requirejs (version 2.3.6) setup is as follows: 我的requirejs(版本2.3.6)设置如下:

<script src="{% static 'main/js/require.js' %}"></script>
<script>
function requireConfig(urlArgs) {
    requirejs.config({
            baseUrl: '/static/main/js/',
            urlArgs: urlArgs,
            waitSeconds: 15,
            packages: [{
                name: 'moment',
                location: '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0',
                main: 'moment'
            }],
            paths: {
                // vendors
                'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min',
                'jquery_ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min' ,
                'text_contrast': './vendors/text_contrast',
            },
            shim: {
                'jquery_ui': {
                    deps: ['jquery']
                },
                'text_contrast': {
                    exports: 'hex',
                },
            }
    });
}
</script>
<script type="text/javascript">
    requireConfig("bust=v18");
    require(['platform/init'], function (m) {
        m.init({
            debug: {{ debug|yesno:"true,false" }},
        });
    });
</script>

I include this before the closing body tag. 我在结束正文标签之前添加了此标签。 Whenever the user refreshes the page after clearing his cache, all (new) js files with the bust argument are loaded correctly. 每当用户在清除缓存后刷新页面时,所有带有bust参数的(新)js文件都会正确加载。 The problemen is, I cant force my users to clear its cache everytime I deploy some new js. 问题是,每次部署一些新的js时,我都无法强迫用户清除其缓存。

Update 更新资料

If I add +(new Date()).getTime() to the requireConfig function, the files now get the time argument, which seems to work. 如果我将+(new Date()).getTime()到requireConfig函数中,文件现在将获得time参数,这似乎可以正常工作。 But on some urls the browser still looks for a js file at the wrong path. 但是在某些URL上,浏览器仍然在错误的路径下查找js文件。 For example when I go to an url it loads: 例如,当我转到一个URL时,它会加载:

/static/main/js/text_contrast.js?bust=v181560938449189 

which gives a 404. After clearing my cache and a reload the file is found at: 给出404。清除缓存并重新加载后,文件位于:

/static/main/js/vendors/text_contrast.js?bust=v181560939213670

This seems to occur on urls I've already loaded in the past with that wrong path pointing to the text_contrast.js file 这似乎发生在我过去已经加载的URL上,该错误路径指向text_contrast.js文件

You can add time stamp to the urlArgs in the requirejs.config object so that url's will not be cached. 您可以将时间戳记添加到requirejs.config对象中的urlArgs,以便不会缓存url。

One way you can do it is by changing your code from 您可以通过以下方法更改代码之一

requireConfig("bust=v18");

to

requireConfig("bust=v18"+(new Date()).getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM