Labib Ismaiel
Labib Ismaiel

Reputation: 1340

Remove Etag Header in express

I have searched a lot and still couldn't find a solution, I am using nodejs with express which is setting etag to true by default, I tried all of the solutions i found online and it is still set, examples:

res.set('etag', false);
res.removeHeader('ETag');
app.disable('etag');
app.use(express.static(__dirname + '/public'), { etag: false });

And still it is set, so, is there something i am missing here since i am not really that experienced in node or express.

My question is obviously, how to disable this header, because, I have a page with a lot of images (A LOT) and all of them are static and etag is causing a lot of blocking since it's sending requests to check validity and preventing the browser from relying on cache-control, which is hugely increasing page load time.

Thanks for the help

Upvotes: 4

Views: 3143

Answers (2)

Labib Ismaiel
Labib Ismaiel

Reputation: 1340

This is not a full answer but I am adding it just in case anyone faces the same issue.

It turned out what I was missing is that the browser forces a cache validity check on first load (including page refresh), and that's why I kept seeing the etag header.

To properly test if the header is removed you have to browse to the url and check not go directly to it.

I hope this helps someone, because it took me a while to find it out

Upvotes: 0

Schahriar SaffarShargh
Schahriar SaffarShargh

Reputation: 1971

Refer to: http://expressjs.com/4x/api.html#app.set

You can do it in ExpressJS 4 using:

app.set('etag', false);

Setting it to false disables the etag header altogether while the default is set to true.

Possible option values are:

  • Boolean (true,false)
  • String ('strong', 'weak')
  • Function

Upvotes: 2

Related Questions