Skip to content

Commit 763fdd1

Browse files
committed
Added more hardcore caching fixes socketio#558
Added tests against it Added vary header for gzip
1 parent 8107c1a commit 763fdd1

File tree

3 files changed

+1993
-6
lines changed

3 files changed

+1993
-6
lines changed

lib/static.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var mime = {
4242
* @api private
4343
*/
4444

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

4848
/**
@@ -121,10 +121,14 @@ Static.prototype.init = function () {
121121
build(self.manager.get('transports'), callback);
122122
});
123123

124+
this.add('/socket.io.v', { mime: mime.js }, function (path, callback) {
125+
build(self.manager.get('transports'), callback);
126+
});
127+
124128
// allow custom builds based on url paths
125129
this.add('/socket.io+', { mime: mime.js }, function (path, callback) {
126130
var available = self.manager.get('transports')
127-
, matches = bundle.exec(path)
131+
, matches = path.match(bundle)
128132
, transports = [];
129133

130134
if (!matches) return callback('No valid transports');
@@ -218,7 +222,7 @@ Static.prototype.has = function (path) {
218222
, i = keys.length;
219223

220224
while (i--) {
221-
if (!!~path.indexOf(keys[i])) return this.paths[keys[i]];
225+
if (-~path.indexOf(keys[i])) return this.paths[keys[i]];
222226
}
223227

224228
return false;
@@ -310,15 +314,16 @@ Static.prototype.write = function (path, req, res) {
310314

311315
// see if we need to set Expire headers because the path is versioned
312316
if (versioned) {
313-
var expires = self.manager.get('browser client expires')
314-
headers['Cache-Control'] = 'public, max-age=' + expires;
317+
var expires = self.manager.get('browser client expires');
318+
headers['Cache-Control'] = 'private, x-gzip-ok="", max-age=' + expires;
315319
headers['Date'] = new Date().toUTCString();
316320
headers['Expires'] = new Date(Date.now() + (expires * 1000)).toUTCString();
317321
}
318322

319323
if (gzip && reply.gzip) {
320324
headers['Content-Length'] = reply.gzip.length;
321325
headers['Content-Encoding'] = 'gzip';
326+
headers['Vary'] = 'Accept-Encoding';
322327
write(200, headers, reply.gzip.content, mime.encoding);
323328
} else {
324329
headers['Content-Length'] = reply.length;

test/static.test.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module.exports = {
2424
, io = sio.listen(port);
2525

2626
(!!io.static.has('/socket.io.js')).should.be.true;
27-
(!!io.static.has('/socket.io+')).should.be.true;
27+
(!!io.static.has('/socket.io.v1.0.0.js')).should.be.true;
28+
(!!io.static.has('/socket.io+xhr-polling.js')).should.be.true;
29+
(!!io.static.has('/socket.io+xhr-polling.v1.0.0.js')).should.be.true;
2830
(!!io.static.has('/static/flashsocket/WebSocketMain.swf')).should.be.true;
2931
(!!io.static.has('/static/flashsocket/WebSocketMainInsecure.swf')).should.be.true;
3032

@@ -472,5 +474,50 @@ module.exports = {
472474
io.server.close()
473475
done();
474476
});
477+
},
478+
479+
'test that a versioned client is served': function (done) {
480+
var port = ++ports
481+
, io = sio.listen(port)
482+
, cl = client(port);
483+
484+
cl.get('/socket.io/socket.io.v0.8.9.js', function (res, data) {
485+
res.headers['content-type'].should.eql('application/javascript');
486+
res.headers['content-length'].should.match(/([0-9]+)/);
487+
res.headers['cache-control']
488+
.indexOf(io.get('browser client expires')).should.be.above(-1);
489+
490+
data.should.match(/XMLHttpRequest/);
491+
492+
cl.end();
493+
io.server.close();
494+
done();
495+
});
496+
},
497+
498+
'test that a custom versioned build client is served': function (done) {
499+
var port = ++ports
500+
, io = sio.listen(port)
501+
, cl = client(port);
502+
503+
io.set('browser client expires', 1337);
504+
505+
cl.get('/socket.io/socket.io+websocket.v0.8.10.js', function (res, data) {
506+
res.headers['content-type'].should.eql('application/javascript');
507+
res.headers['content-length'].should.match(/([0-9]+)/);
508+
res.headers['cache-control']
509+
.indexOf(io.get('browser client expires')).should.be.above(-1);
510+
511+
data.should.match(/XMLHttpRequest/);
512+
data.should.match(/WS\.prototype\.name/);
513+
data.should.not.match(/Flashsocket\.prototype\.name/);
514+
data.should.not.match(/HTMLFile\.prototype\.name/);
515+
data.should.not.match(/JSONPPolling\.prototype\.name/);
516+
data.should.not.match(/XHRPolling\.prototype\.name/);
517+
518+
cl.end();
519+
io.server.close();
520+
done();
521+
});
475522
}
476523
};

0 commit comments

Comments
 (0)