Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,24 @@ function createEventStream(heartbeat) {
}, heartbeat).unref();
return {
handler: function(req, res) {
req.socket.setKeepAlive(true);
res.writeHead(200, {
var headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream;charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
// While behind nginx, event stream should not be buffered:
// http://nginx.org/docs/http/ngx_http_proxy_module.html#proxy_buffering
'X-Accel-Buffering': 'no'
});
};

var isHttp1 = !(parseInt(req.httpVersion) >= 2);
if (isHttp1) {
req.socket.setKeepAlive(true);
Object.assign(headers, {
'Connection': 'keep-alive',
});
}

res.writeHead(200, headers);
res.write('\n');
var id = clientId++;
clients[id] = res;
Expand Down
10 changes: 10 additions & 0 deletions test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ describe("middleware", function() {
}
});
});
// Express HTTP/2 support is in progress: https://github.com/expressjs/express/pull/3390
it("should not contain `conntection: keep-alive` header for HTTP/2 request");
it("should contain `conntection: keep-alive` header for HTTP/1 request", function(done) {
request('/__webpack_hmr')
.end(function(err, res) {
if (err) return done(err);
assert.equal(res.headers['connection'], 'keep-alive');
done();
});
});
});

beforeEach(function() {
Expand Down