express-interceptor
express-interceptor copied to clipboard
ETags don't reflect modified content
The interception appears to happen after ETag header has already been set for the response. Consequently, the ETag doesn't reflect the changed output, which can cause browser caching issues--even if the content is wildly different from one page load to the next, the server is telling the browser, "Nah, nothing changed since the last time you loaded this resource."
It's pretty simple (even if a little inefficient) to regenerate the ETag with another library (e.g., https://www.npmjs.com/package/etag) right before calling send(). But the whole ETag thing a hazard that most users are unlikely to recognize. Would be great to have a built-in option. Or at least a warning in the docs/examples
We got hit by an ETag issue where every other request would fail to intercept. This was made even more difficult to diagnose because of the Disable Cache
checkbox being checked in Chrome DevTools; The problem would go away when DevTools was open.
The issue we were having was that the interceptor would encounter an empty body because of the 304 Not Modified
statusCode.
Our solution was to check the status code in the isInterceptable
method.
isInterceptable() {
// Don't intercept cached responses (etag)
return res.statusCode !== 304
},
Alternatively, you can check the body in the intercept
method.
intercept(body, send) {
if (!body) {
send(body)
} else {
const stuff = JSON.parse(body)
// do something...
send(JSON.stringify(stuff))
}
},
Finally, if you don't want to write any of these checks and want to disable the server cache entirely via a config.
app.set('etag', false)