3rd-Eden / versions

Versions, A small module for creating a flexible CDN application
204 stars 13 forks source link

Allow custom expiration per content type #9

Open 3rd-Eden opened 11 years ago

3rd-Eden commented 11 years ago
// public api
versions.set('max age', {
  'image/gif': '10 hours',
  'text/javascript': '5 hours'
});

This allows a much more flexible caching then currently available.

davidmurdoch commented 11 years ago

I just spent a bit of time on this and it looks as though connect's static makes this very difficult, which I guess is part of the reason https://github.com/3rd-Eden/versions/issues/4 already exists. haha.

Anyway, I don't really see any way to do this sanely without first rewriting static, but here is the code I came up with for recursively parsing the new format for the max age option:

Versions.prototype.parse = function parse(nr, options) {
  if (+nr && !options) return +nr;
  if ( 'object' !== typeof nr ){
    return ms(nr, options);
  }

  Object.keys(nr).forEach(function (key) {
    this[key] = parse(this[key], options);
  }, nr);
  return nr;
};