Node.js express caching js files

I use a simple express to work with the base project. This is the file 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 !!!"); }); 

Each deployment project is optimized using require.js (r.js). Files are concretized, minimized and quenched and deployed on a production server.

The problem is as follows. Suppose I set the cache for 15 days. And on the third day we are deploying a new project, that is, a new version. The client browser will not pull out new javascript files, as it is already cashed and valid.

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

Thanks: D

+6
source share
2 answers

Unable to clear browser cache from server. This is why many frameworks add a version line after the URL for static content.

If your application version is 3.0.1, your static url should look like this:

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

The npm repository has an available module called versionator :

+3
source

I found a simpler solution and then used the version file.

Since I used require.js for the project, it turns out that require.js has a built-in method for managing javascript files: I use: "urlArgs:" bust = v3 "," ...

Segment from main.js file:

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

All Articles