作者:ik人生如梦场 | 来源:互联网 | 2023-05-19 05:20
Thisquestionhasbeenaskedseveraltimesbutnotspecifictothisexample.这个问题已被多次询问,但不是特定于这个例子。
This question has been asked several times but not specific to this example.
这个问题已被多次询问,但不是特定于这个例子。
Here is an overview of our application:
以下是我们的应用程序概述:
- Simple server routing with Express on Node
- 使用Express on Node进行简单的服务器路由
- Single page backbone application
- 单页骨干应用程序
- Core modules and libraries (JS/CSS) that don't change
- 核心模块和库(JS / CSS)不会改变
- Widget JS/LESS/HTML files that frequently change
- 经常更改的小部件JS / LESS / HTML文件
During development, I am looking to cache bust files that change but not those core libraries to speed up my page reloads and quicken my development.
在开发过程中,我希望缓存更改胸部文件而不是那些核心库,以加快我的页面重新加载并加快我的开发速度。
I have found explanations on:
我找到了解释:
- Cache Busting on RequireJS - here
- 在RequireJS上缓存Busting - 这里
- Explanation on Cache Busting Specific Modules - here
- Cache-Busting Specific Modules的解释 - 这里
Could the solution:
可以解决方案:
- Explain how the requireJS caching busting works?
- 解释requireJS缓存破坏是如何工作的?
- Provide a solution for this specific scenario.
- 为此特定方案提供解决方案。
2 个解决方案
4
The cache-busting works by appending an always-unique query string to the end of every file which is required. It makes use of RequireJS's urlArgs
config value; RequireJS takes care of appending it for you:
缓存清除的工作原理是将一个始终唯一的查询字符串附加到所需的每个文件的末尾。它利用了RequireJS的urlArgs配置值; RequireJS负责为您追加:
urlArgs: "bust=" + (new Date()).getTime()
The(new Date()).getTime()
part is just a simple way to get a unique string out of Javascript. You could do some variation on Math.random()
, but using the number of milliseconds since the epoch guarantees uniqueness, for optimum cache-bustage.
(new Date())。getTime()部分只是一种从Javascript中获取唯一字符串的简单方法。你可以在Math.random()上做一些变化,但是使用自纪元以来的毫秒数保证了唯一性,以获得最佳的缓存缺陷。
I think Mr Burke is suggesting something like:
我认为伯克先生建议的事情如下:
require.config({
baseUrl: '/base/path',
paths: {
'fileAlias': 'fileLikelyToChange?bust=' + (new Date()).getTime(),
'anotherFileAlias': 'anotherFileLikelyToChange?bust=' + (new Date()).getTime(),
'jQuery': 'jQuery'
},
});
So, instead of the ubiquitous urlArgs
cache-busting, you apply it specifically to each file which is likely to change; hence, excluding any libraries.
因此,不是无处不在的urlArgs缓存破坏,而是将其专门应用于可能会发生变化的每个文件;因此,不包括任何图书馆。
I haven't tested it, but I'd probably tidy it up to something like:
我没有测试过,但我可能会把它整理成类似的东西:
function bust(path) {
return path + '?bust=' + (new Date()).getTime();
}
require.config({
baseUrl: '/base/path',
paths: {
'fileAlias': bust('fileLikelyToChange'),
'anotherFileAlias': bust('anotherFileLikelyToChange'),
'jQuery': 'jQuery'
},
});