Friday, January 30, 2009

GWT, Internet Explorer and XMLHttpRequest Caching

This morning I spent over an hour tracking down and solving a client/server communication issue which I'm going to document here, for all weary travellers who come this way. The application in question uses the GWT RequestBuilder in order to make an HTTP GET request to the server. When running in GWT Hosted mode I have a relay servlet that acts as a proxy allowing me to test the application against the real server without breaking the browser same origin policy.

The Problem

When running the application I couldn't understand why the first request retrieved the correct JSON response, but any further requests only ever returned this same original response, when in fact they should have been very different. I tried all the obvious things such as accessing the server using a browser and the server appeared to be working correctly.

I finally tracked the issue down to the fact that on Vista GWT hosted mode uses Internet Explorer as its embedded browser. Unfortunately due to some bugs (thanks to this post which gave me the required pointers), Internet Explorer will cache XMLHttpRequests if they're for the same URL. As it happens, because I'm using a relay servlet, all the requests do indeed look like they are going to the same URL, and thus IE keeps returning the same response, without ever sending the request to the server.

The Solution

The solution (at least when using hosted mode), is to change the Internet Explorer cache settings (I'm using IE 7), which can be done by selecting Tools, Options and clicking the Browser History settings button, (I'm not sure why cache settings are in browser history section!):

In the resulting dialog, chosing "Check for newer pages Every time I visit a page", resolved the issue. I would certainly recommend modifying this setting even if your not using XMLHttpRequests as I've also had issues with style sheets getting stuck in the IE cache!

Note: this appears to be a Windows only issue, on Linux GWT hosted mode uses Mozilla, although it certainly would be nice to choose either IE or Mozilla on windows!

2 comments:

Unknown said...

Hi,
I ran into your post a little while after solving my problem and I just wanted to comment something, that maybe goes without saying, which is if you need your web-app to function properly on IE in production mode than your solution is problematic as you cannot instruct your clients to use this setting. Instead my current work-around, which I'm not sure is final, is to attach a dummy parameter to the URL with a Random number which then of course seems to IE as a different url.

Thanks anyway

Dean Povey said...

Also ran across this problem, but I used a different solution which was to set the If-Modified-Since header on the GET request to the epoch date. This means that you don't get your cache filling up with a bunch of requests you are never going to look at. See: http://thinking-in-code.blogspot.com/2010/06/preventing-internet-explorer-from.html