Skip to content
Closed
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
5 changes: 4 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ res.send = function send(body) {
}

// determine if ETag should be generated
// Skip ETag for no-store responses as the client will ignore it anyway
var etagFn = app.get('etag fn')
var generateETag = !this.get('ETag') && typeof etagFn === 'function'
var cacheControl = this.get('Cache-Control')
var isNoStore = cacheControl && /(?:^|,)\s*no-store\s*(?:,|$)/i.test(cacheControl)
var generateETag = !isNoStore && !this.get('ETag') && typeof etagFn === 'function'

// populate Content-Length
var len
Expand Down
36 changes: 36 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,42 @@ describe('res', function(){
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
})

it('should not send ETag when Cache-Control: no-store is set', function (done) {
var app = express();

app.use(function (req, res) {
res.set('Cache-Control', 'no-store');
var str = Array(1000).join('-');
res.send(str);
});

app.enable('etag');

request(app)
.get('/')
.expect('Cache-Control', 'no-store')
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
})

it('should not send ETag when Cache-Control contains no-store', function (done) {
var app = express();

app.use(function (req, res) {
res.set('Cache-Control', 'private, no-store, max-age=0');
var str = Array(1000).join('-');
res.send(str);
});

app.enable('etag');

request(app)
.get('/')
.expect('Cache-Control', 'private, no-store, max-age=0')
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
})
});

describe('when disabled', function () {
Expand Down