express returns 304 for IE repeative requests

2019-03-18 16:04发布

I'm experiencing some strange behavior of ExpressJS. On second request to my node.js/express based API URL it always returns 304 Not Modified response code to IE. Other browsers get 200(Chrome/FF). The problem is, that it returns 304 even if the content actually has been changed. I tried to search, and couldn't find anything on the topic. Also I tried to find a difference in requests headers of IE and Chrome, and could see any header that may cause that. Any help will be appreciated.

I have to add the connection goes via SSL, in case it matters

3条回答
淡お忘
2楼-- · 2019-03-18 16:41

The Cache-Control header is a workaround. The bug is in internet explorer's interpretation of the HTTP 1.1 spec for the header.

I added this to my route handler, which solved the problem. You also need a Last-Modified or ETag header, but express was already sending that for me.

res.setHeader("Expires", "-1");
res.setHeader("Cache-Control", "must-revalidate, private");

See: Make IE to cache resources but always revalidate

查看更多
贼婆χ
3楼-- · 2019-03-18 16:42

Well, I managed to fix it by adding Cache-Control header

查看更多
干净又极端
4楼-- · 2019-03-18 16:45

Having the same problem I look a lot around and it turns that in fact the problem comes from a stupid aggressive caching of ajax get requests by IE. In fact when you see this 304, the actual request never hits the server but IE responds with the latest data from his cache. This is intended behavior by MS, and thus there are only workarounds.

My preferred is to attach to each ajax get request a useless query parameter containing the current time. It will force IE to always retrieve from the server. The nice part is if you use jQuery, you can configure it to this automatically with

$.ajaxSetup({cache:false})

Another work around is to use POST requests instead of GET, but this is not always an option.

查看更多
登录 后发表回答