Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
First stab of adding Expires + cache control headers for #558
  • Loading branch information
3rd-Eden committed Oct 5, 2011
commit 5573f7fcdfa18d1833e01a90c5502fb2132ad5f7
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