expressjs / serve-static

Serve static files
MIT License
1.38k stars 227 forks source link

Use seconds instead of milliseconds for maxAge #150

Closed naz closed 1 year ago

naz commented 1 year ago

The max-age value in cache-control is supposed to be passed in in seconds (max-age spec.) The middleware accepts milliseconds for maxAge paramter, which creates a bit of confusion around conversion from one unit to another.

As a reference where seconds are used for max-age - the CORS middleware passes along seconds instead of milliseconds.

Not sure what's the best way to disambiguate the situation and start using seconds. Maybe allowing to pass in an additional property "seconds: true" to the middleware could be one of the approaches. Not sure. Just found this behavior confusing and have cought some major bugs that in the codebase that were coming out of the second/millisecond confusion.

dougwilson commented 1 year ago

Yea, I certainly agree with you, but it's going to be very difficult as even if it's changed with a major version bump all the examples and videos and blogs (that wr don't have control over) out there will still work and people will end up even more confused. We already tried once. The best thing to do to disambiguare is to use a string instead: '40s' for 40 seconds.

The best advice of course is folks should be reading the docs (https://github.com/expressjs/serve-static#maxage) for modules they are using, or at least test the functionality they are implementing (would realize their cache has wrong time), but I understand that is not always feasible. Unfortunately for some changes like this the uphill battle against the sheer content out there is too great to overcome some past decisions.

But ultimately there is no need to add a second seconds: true parameter as you can already use the steing form as above to pass in seconds 👍

naz commented 1 year ago

Thanks @dougwilson, makes total sense 👍 It's one of those pandora boxes that are hard to close once open it seems. Hopefully this issue makes millisecond/second difference more visible and folks don't get trapped as much. Cheers!