最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js express JS file caching - Stack Overflow

programmeradmin5浏览0评论

I am using simple express to serve Backbone project. 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). Files are Concenated, minified and uglified and deployed to production server.

Problem is the following. Assume I set the cache for 15 days. And on day 3 we deploy new project, meaning new version. Client browser will not pull new javascript's files, as it already has them cashed and valid.

How do I trick the browser to remove its cache with new files?

Thanks :D

I am using simple express to serve Backbone project. 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). Files are Concenated, minified and uglified and deployed to production server.

Problem is the following. Assume I set the cache for 15 days. And on day 3 we deploy new project, meaning new version. Client browser will not pull new javascript's files, as it already has them cashed and valid.

How do I trick the browser to remove its cache with new files?

Thanks :D

Share Improve this question asked Dec 24, 2013 at 16:02 EnterpriseXLEnterpriseXL 4778 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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.

If your app version is 3.0.1, your static content url should be like this:

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

There is an available module for you named versionator on npm repository:

  • https://github./serby/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","...

Segment from my main.js file:

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

评论列表(0)

  1. 暂无评论