express-http-proxy icon indicating copy to clipboard operation
express-http-proxy copied to clipboard

Regenerate etag header if response body may have changed

Open alexb-uk opened this issue 5 years ago • 0 comments

Change

Clear the etag header to force Express to regenerate the etag as body may have changed. This prevents 304 Not Modified when downstream response is unchanged but userResDecorator has.

Background

We found an issue in our live deployment where the client was using stale cached responses. The issue was the downstream web server responding with an etag header based on it's response body which was unchanged.

express-http-proxy proxies this etag header back to the client even when userResDecorator may have changed the response. In our scenario the amendment to the response is itself dynamic.

The fix was straightforward but I thought this gotcha could be affecting many people without their knowledge.

Workaround without changing express-http-proxy

const ResetEtagHeader = (headers: IncomingHttpHeaders): OutgoingHttpHeaders => {
  // Clear the etag header so Express will regenerate it
  headers.etag = '';
  return headers;
};

proxy(serverURL, {
      userResDecorator: DynamicDecoratorBlah,
      userResHeaderDecorator: ResetEtagHeader,
});

alexb-uk avatar Jul 28 '20 22:07 alexb-uk