expressjs / serve-static

Serve static files
MIT License
1.39k stars 228 forks source link

"Cache-Control: public, max-age=0" is set by default #66

Closed SVasilev closed 8 years ago

SVasilev commented 8 years ago

Hello, I am using this module for a while and I encountered the following problem. I cannot remove the Cache-Control header. It is always set by default to some value. I noticed that this behaviour comes from the "send" module, which you use:

if (!res.getHeader('Cache-Control')) {
  res.setHeader('Cache-Control', 'public, max-age=' + Math.floor(this._maxage / 1000));
}

I managed to make a work-around with the "on-headers" module, mentioned in issue https://github.com/expressjs/serve-static/issues/5 which looks like this:

var onHeaders = require('on-headers');
onHeaders(function() {
  this.removeHeader('Cache-Control');
}

But this approach looks pretty sketchy. So the question is why can't this be configured somewhere in the options, because after researching Apache and Nginx I saw that they don't add the Cache-Control header by default and I want to accomplish that behaviour. Besides it looks like the right approach to me because after looking at the HTTP1.1 spec I found:

Unless specifically constrained by a cache-control (section 14.9) directive, a caching system MAY always store a successful response (see section 13.8) as a cache entry, MAY return it without validation if it is fresh, and MAY return it after successful validation.

So, it seems that is up to the Content Delivery Networks (Akamai, etc) to decide whether something is cachable by default. So is there a reason for forcing the Cache-Control by default in this module?

dougwilson commented 8 years ago

Hi @SVasilev, can you open an issue at https://github.com/pillarjs/send/issues? This module doesn't have any code actually setting the Cache-Control header, so though we can discuss here, we can't really resolve the issue here. We would like to have the discussion in the same repository that the code change would actually happen for future reference for ourselves and users, I hope you understand :)

dougwilson commented 8 years ago

Sorry, I didn't mean to close this issue, as we can keep it open while discussion happens in the send repository (so we can involve the jshttp group, who oversees the send module).

SVasilev commented 8 years ago

Hello, Sorry for the late reply. I opened the issue as you told me: https://github.com/pillarjs/send/issues/110 Best regards, Stefan