简体   繁体   English

node.js表达JS文件缓存

[英]node.js express JS file caching

I am using simple express to serve Backbone project. 我使用简单快递来服务Backbone项目。 This is the file that we use: 这是我们使用的文件:

 app.configure('production', function () {
 var myTime = 432000;         //5 days
 app.set('port', process.env.PORT || 80);
 app.use(express.bodyParser());
 app.use(express.methodOverride());
 app.use(allowCrossDomain);
 app.use(app.router);    
 app.use(express.static(__dirname + './../',  { maxAge: myTime } ));
 console.log("SERVING !!!");
});

Every deployment project is optimized using require.js (r.js). 每个部署项目都使用require.js(r.js)进行优化。 Files are Concenated, minified and uglified and deployed to production server. 文件是Concenated,minified和uglified并部署到生产服务器。

Problem is the following. 问题如下。 Assume I set the cache for 15 days. 假设我将缓存设置为15天。 And on day 3 we deploy new project, meaning new version. 在第3天,我们部署了新项目,意味着新版本。 Client browser will not pull new javascript's files, as it already has them cashed and valid. 客户端浏览器不会提取新的javascript文件,因为它已经兑现了它们并且有效。

How do I trick the browser to remove its cache with new files? 如何欺骗浏览器删除新文件的缓存?

Thanks :D 感谢:D

There is no way to clear browser cache from the server. 无法从服务器清除浏览器缓存。 That is why many frameworks add a version string after url for static content. 这就是为什么许多框架在url之后为静态内容添加版本字符串的原因。

If your app version is 3.0.1, your static content url should be like this: 如果您的应用版本是3.0.1,则您的静态内容网址应如下所示:

http://example.com/js/jquery.js?v=3.0.1 http://example.com/js/jquery.js?v=3.0.1

There is an available module for you named versionator on npm repository: 在npm存储库上有一个名为versionator的可用模块:

I have found simpler solution then using versionator. 我找到了更简单的解决方案然后使用版本控制器。

Since I used require.js for the project, it turns out that require.js has built in method to control javascript files: I am using: " urlArgs: "bust=v3","... 由于我在项目中使用了require.js,结果发现require.js内置了控制javascript文件的方法:我正在使用:“urlArgs:”bust = v3“,”...

Segment from my main.js file: 来自我的main.js文件的细分:

require.config({
baseURL: '.',
urlArgs: "bust=v3",
paths: {
    underscore  : 'lib/underscore',
    backbone    : 'lib/backbone',
    babysitter  : 'lib/backbone.babysitter',

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

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