How to remove Etag and Last-Modified headers for static assets?

I want to disable Etag and Last-Modified headers for index.html files served by express.static, how can I accomplish this?

I tried:

app.use(express.static('public', {
    setHeaders: function(res, path){
        if (path.match(/index\.html$/)){
            res.removeHeader('Etag')
            res.removeHeader('Last-Modified')
        }
    }
}));

But it doesn’t work, Etag and Last-Modified still in response’s headers.

Author: Fantashit

1 thought on “How to remove Etag and Last-Modified headers for static assets?

  1. For ETag, just set etag: false in the options:

    app.use(express.static('public', {etag: false})

    The send module (which is used behind the scenes) does not provide an option to not set the Last-Modified header (I can make it do that in time). You would have to use on-headers to unset Last-Modified OR if you only want to not send these headers for a certain path:

    var onHeaders = require('on-headers')
    
    function unsetCacheHeaders() {
      this.removeHeader('Etag')
      this.removeHeader('Last-Modified')
    }
    
    app.use(express.static('public', {
      setHeaders: function (res, path) {
        // no etag or last-modified for index.html files
        if (path.substr(10) === 'index.html') {
          onHeaders(res, unsetCacheHeaders)
        }
    }))

Comments are closed.