Extension for nun for static content versioning
JavaScript
Switch branches/tags
Nothing to show
Latest commit 80d39ec Oct 29, 2010 @akaspin Remove sys from readme
Permalink
Failed to load latest commit information.
.gitignore
README.md
index.js

README.md

nun-v

nun-v is extension for nun templating engine for static content versioning inspired with tornado static files and aggressive file caching.

How it works?

For example, you can add following settings in nginx.conf:

location /static/ {
    root /var/www/static;
        if ($query_string) {
        expires max;
    }
}

And if the link takes the following form:

/static/image.jpg?v=hash

... browser will use a locally cached copy without ever checking for updates on the server.

Usage

nun-v provides asynchronous compile-phase filter for rarely changing files.

Template:

<h1>Static image</h1>
<img src="{{~v}}title.jpg{{/v}}" />

Code:

var nun = require('nun');
var nunV = require('nun-v');

var filters = {
    v: nunV.filter(__dirname + "/static", "/static"),
};

var origin = __dirname + "/template.html";

nun.render(origin, {}, {filters:filters}, function(err, output){
    if (err) throw err;
    
    var buffer = '';
    output.addListener('data', function(data){ buffer += data; })
          .addListener('end', function(){ console.log(buffer) });
});

Output:

<h1>Static image</h1>
<img src="/title.jpg?v=123456" />

... and

Since the v argument is based on the content of the file, if you update a file and restart your server, it will start sending a new v value, so the user's browser will automatically fetch the new file. If the file's contents don't change, the browser will continue to use a locally cached copy without ever checking for updates on the server, significantly improving rendering performance.