15

At times during development, it would be really nice to prevent HTTP 304 responses (in favor of 200's), and cause the Connect/Express static middleware to read every response from the filesystem, rather than do any caching at all.

I have tried playing with maxAge values of 0 and 1, to no avail:

app.use(express.static(__dirname + '/public', { maxAge: 1 }))
Jacob Marble
  • 24,696
  • 18
  • 62
  • 76

4 Answers4

19

I get 200 responses by doing this during development :

var express = require('express');
app = express();
app.use(function(req, res, next) {
  req.headers['if-none-match'] = 'no-match-for-this';
  next();    
});
Mark Selby
  • 557
  • 5
  • 7
  • What are the repercussions for using this on a production environment? I'm using it for development, but I'm afraid if I remove it my users will have the same problem. – Hawkee Aug 05 '14 at 19:19
  • This is perfect for development. I believe that if you want dynamic behaviour in production, one should serve the files accordingly by creating a separate route. Real world projects would need both the 304 working for really static resources and a way of allowing dynamic loading. – Andrei Dec 09 '14 at 14:52
8
app.disable('etag');

preventing 'etag' in response may help

Mohan R
  • 91
  • 1
  • 2
  • 1
    Where do we add this ? – SharpCoder Aug 19 '15 at 09:47
  • No it doesn't, because Express will still calculate the ETag and compare it, just not return it. So if the server previous did return an ETag, users sending that tag will now still get a 304 even after you made this change. See https://github.com/expressjs/express/pull/2841/ – CherryDT Dec 16 '20 at 12:57
6

it does read from the file system on every response. it's just that if the request ETAG matches the response ETAG, it doesn't send the body of the response because it doesn't have to . It's the same file with the same hash. this is how 304 responses work.

why do you want to prevent 304 responses?

Jonathan Ong
  • 17,958
  • 15
  • 74
  • 113
0

This solution is just a workaround. You could solve the problem from the browser side by disabling caching in Chrome. This doesn't help you if you need to work on something outside of Chrome, like Safari on iOS.

Community
  • 1
  • 1
Jacob Marble
  • 24,696
  • 18
  • 62
  • 76
  • 1
    I dont see why this is accepted answer as service consumers are not browsers only but can be other services or 3rd party software – Mladen Oršolić Mar 09 '18 at 10:33