How to totally prevent HTTP 304 responses in Connect/Express static middleware?
Asked Answered
G

4

17

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 }))
Growing answered 1/2, 2013 at 7:2 Comment(0)
E
20

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();    
});
Emelinaemeline answered 3/10, 2013 at 20:32 Comment(2)
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.Demello
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.Liken
M
9
app.disable('etag');

preventing 'etag' in response may help

Myrwyn answered 16/3, 2014 at 6:20 Comment(2)
Where do we add this ?Sankhya
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 github.com/expressjs/express/pull/2841Khelat
T
5

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?

Trencherman answered 13/2, 2013 at 0:59 Comment(2)
It has confused the hell out of me as to why Express would return 304s. Thank you!Nolen
Because weak etags cache JS files which may be a pain at frontend dev dude.Mokas
G
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.

Growing answered 1/2, 2013 at 7:2 Comment(1)
I dont see why this is accepted answer as service consumers are not browsers only but can be other services or 3rd party softwareHyper

© 2022 - 2024 — McMap. All rights reserved.