Skip to content

Commit

Permalink
First stab of adding Expires + cache control headers for socketio#558
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Oct 5, 2011
1 parent 1d743cf commit 5573f7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function Manager (server, options) {
, 'browser client cache': true
, 'browser client minification': false
, 'browser client etag': false
, 'browser client expires': 315360000
, 'browser client gzip': false
, 'browser client handler': false
, 'client store expiration': 15
Expand Down
14 changes: 12 additions & 2 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ var mime = {
* @api private
*/

var bundle = /\+((?:\+)?[\w\-]+)*(?:\.js)$/g;
var bundle = /\+((?:\+)?[\w\-]+)*(?:\.v\d+\.\d+\.\d+)?(?:\.js)$/g
, versioning = /\.v\d+\.\d+\.\d+(?:\.js)$/;

/**
* Export the constructor
Expand Down Expand Up @@ -291,15 +292,23 @@ Static.prototype.write = function (path, req, res) {
var accept = req.headers['accept-encoding'] || ''
, gzip = !!~accept.toLowerCase().indexOf('gzip')
, mime = reply.mime
, versioned = reply.versioned
, headers = {
'Content-Type': mime.type
};

// check if we can add a etag
if (self.manager.enabled('browser client etag') && reply.etag) {
if (self.manager.enabled('browser client etag') && reply.etag && !versioned) {
headers['Etag'] = reply.etag;
}

// see if we need to set Expire headers because the path is versioned
if (versioned) {
var expires = self.manager.get('browser client expires')
headers['Cache-Control'] = 'public, max-age=' + expires;
headers['Expires'] = new Date(Date.now() + (expires * 1000)).toUTCString();
}

// check if we can send gzip data
if (gzip && reply.gzip) {
headers['Content-Length'] = reply.gzip.length;
Expand Down Expand Up @@ -342,6 +351,7 @@ Static.prototype.write = function (path, req, res) {
, length: content.length
, mime: details.mime
, etag: etag || client.version
, versioned: versioning.test(path)
};

// check if gzip is enabled
Expand Down

0 comments on commit 5573f7f

Please sign in to comment.