热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用RequireJS缓存破坏特定模块-CachebustingspecificmoduleswithRequireJS

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:

我找到了解释:

  1. Cache Busting on RequireJS - here
  2. 在RequireJS上缓存Busting - 这里
  3. Explanation on Cache Busting Specific Modules - here
  4. Cache-Busting Specific Modules的解释 - 这里

Could the solution:

可以解决方案:

  1. Explain how the requireJS caching busting works?
  2. 解释requireJS缓存破坏是如何工作的?
  3. Provide a solution for this specific scenario.
  4. 为此特定方案提供解决方案。

2 个解决方案

#1


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'
    },
});

#2


0  

Just remember that if you really need to rely on some external script that you can use $.getScript instead of require to ensure that it's included. I have some non-amd scripts that are for 3rd party integration (e.g. amazon payments), which I used getScript for instead of require. If you can use this method, it would avoid sending the cache busting parameters from urlArgs to the external server.

请记住,如果你真的需要依赖一些外部脚本,你可以使用$ .getScript而不是require来确保它包含在内。我有一些非amd脚本用于第三方集成(例如亚马逊支付),我使用getScript代替require。如果您可以使用此方法,则可以避免将缓存清除参数从urlArgs发送到外部服务器。


推荐阅读
author-avatar
ik人生如梦场
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有