Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js express JS file caching

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

like image 879
EnterpriseXL Avatar asked Dec 24 '13 16:12

EnterpriseXL


1 Answers

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.com/js/jquery.js?v=3.0.1

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

  • https://github.com/serby/versionator
like image 153
damphat Avatar answered Sep 22 '22 10:09

damphat