villadora / express-http-proxy

Proxy middleware for express/connect
Other
1.22k stars 237 forks source link

Regenerate etag header if response body may have changed #463

Closed alexb-uk closed 1 year ago

alexb-uk commented 4 years ago

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 commented 1 year ago

Closing as project appears to be dormant.