防止RequireJS缓存所需的脚本 [英] Prevent RequireJS from Caching Required Scripts

查看:80
本文介绍了防止RequireJS缓存所需的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RequireJS似乎在内部执行缓存所需javascript文件的操作。如果我对其中一个必需文件进行了更改,则必须重命名该文件才能应用更改。

RequireJS seems to do something internally that caches required javascript files. If I make a change to one of the required files, I have to rename the file in order for the changes to be applied.

将版本号作为查询字符串参数附加到文件名末尾的常见技巧不适用于requirejs < script src = jsfile.js?v2>< / script>

The common trick of appending a version number as a querystring param to the end of the filename does not work with requirejs <script src="jsfile.js?v2"></script>

我正在寻找的是一种防止RequireJS内部缓存的方法每次更新脚本文件时都不需要重命名所需的脚本。

What I am looking for is a way to prevent this internal cacheing of RequireJS required scripts without having to rename my script files every time they are updated.

跨平台解决方案:

我现在正在使用 urlArgs:bust =+(new Date())。getTime()用于开发期间的自动缓存清除和 urlArgs:bust = v2用于生产,我在推出更新的必需脚本后增加硬编码版本号。

I am now using urlArgs: "bust=" + (new Date()).getTime() for automatic cache-busting during development and urlArgs: "bust=v2" for production where I increment the hard-coded version num after rolling out an updated required script.

注意:

@Dustin Getz在最近的一个回答中提到Chrome Developer Tools会在调试过程中连续刷新Javascript文件时丢弃断点像这样。一种解决方法是在代码中编写调试器; 以在大多数Javascript调试器中触发断点。

@Dustin Getz mentioned in a recent answer that Chrome Developer Tools will drop breakpoints during debugging when Javascript files are continuously refreshed like this. One workaround is to write debugger; in code to trigger a breakpoint in most Javascript debuggers.

服务器 - 特定解决方案:

对于可能更适合您的服务器环境(如Node或Apache)的特定解决方案,请参阅下面的一些答案。

For specific solutions that may work better for your server environment such as Node or Apache, see some of the answers below.

推荐答案

可以将RequireJS配置为为每个脚本URL附加一个值以进行缓存清除。

RequireJS can be configured to append a value to each of the script urls for cache busting.

来自RequireJS文档( http:/ /requirejs.org/docs/api.html#config ):

From the RequireJS documentation (http://requirejs.org/docs/api.html#config):


urlArgs :额外查询字符串参数附加到RequireJS
用于获取资源的URL。在未正确配置浏览器或
服务器时缓存半身像最有用。

urlArgs: Extra query string arguments appended to URLs that RequireJS uses to fetch resources. Most useful to cache bust when the browser or server is not configured correctly.

示例,将v2附加到所有脚本:

Example, appending "v2" to all scripts:

require.config({
    urlArgs: "bust=v2"
});

出于开发目的,您可以通过附加时间戳强制RequireJS绕过缓存:

For development purposes, you can force RequireJS to bypass the cache by appending a timestamp:

require.config({
    urlArgs: "bust=" + (new Date()).getTime()
});

这篇关于防止RequireJS缓存所需的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆