4

I have a web app. that uses AJAX (via jQuery). I noticed that some users make updates that should automatically update on the web page via $.get, but in some cases no update occurs. I narrowed the problem down to either

A.  A race condition
or 
B.  Caching

I am unable to reproduce the problem on my browsers, IE7 and FF7. I went to the user's machine and had them check the setting to "Check for new versions of a page: Every visit" in IE7. The problem seems to be gone. So, I am confident the problem is this setting, originally set to "Automatic".

Do other browsers support this "caching" (or however this technically translates)?

Any idea why IE7 is not smart enough to let AJAX $.get() requests not be cached when code explicitly makes a server call? I mean, is there a known bug or is this a "feature"?

Can it be disabled without flipping a client setting?

P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327
  • @psr - that question specifically addresses HTTP. I do not know how to translate that solution to one that will work with jQuery. I am also not sure that the solution even applies to this context. But, this makes me wonder if this question really belongs on SO. – P.Brian.Mackey Nov 08 '11 at 19:05
  • @psr - That looks like a good possible solution to my third question. Do you know how the browser setting relates to the problem? Am I to assume that changing the setting as described provides an alternate solution? – P.Brian.Mackey Nov 08 '11 at 19:14
  • @psr The Stack Overflow question is certainly related, but we can't migrate it here. It would be clearly off-topic. – Adam Lear Nov 08 '11 at 20:04
  • I think this question could do well on SO as well. It's partially conceptual, but at the core seems to still be an implementation problem. Would you like me to migrate it over? – Adam Lear Nov 08 '11 at 20:04
  • @Anna Lear - Sure. I think that's a good idea, thank you. – P.Brian.Mackey Nov 08 '11 at 20:14

3 Answers3

4

I guess I should move from comments to an answer.

The way to get JQuery to disable caching for you is covered by this stack overflow question.

Yes other browsers cache, I don't know of any common browser that doesn't support caching. There is some variation in how users set their preferences for this, though.

I believe the caching should be considered a feature, because it's just making a get request, though I honestly don't know if standards address this behavior.

It can't be disabled without flipping a client setting, but the easiest way around it is to make sure it doesn't see the same URL twice, so it can't possibly cache it. This is actually what JQuery does internally anyway if you set the cache parameter to false.

You can control caching via headers, but I don't think all browsers support this properly. I believe right now never repeating a URL is the only reliable cross browser solution.

Community
  • 1
  • 1
psr
  • 2,782
  • 15
  • 22
  • 2
    What browser doesn't understand Cache-Control? Always requesting unique URIs is going to waste space in the browser cache. – Dave Mooney Nov 08 '11 at 20:34
  • @DaveMooney I think some OLD browsers ignored it but even those respected the Pragma nocache header. When I need to ensure no caching is occurring then I always add both out of habit. – maple_shaft Nov 08 '11 at 20:50
  • I don't know which browsers respond to what headers, but for some reason JQuery still implements setting cache to false by appending a time stamp, and it's certainly a heavily reviewed library. Could just be backward compatibility, I guess. See http://api.jquery.com/jQuery.ajax/ under the cache parameter. – psr Nov 08 '11 at 21:04
  • @psr The timestamp technique would also sidestep any proxy caches sitting between the browser and web server. I can see the benefit. – Dave Mooney Nov 10 '11 at 21:49
2

You can set the Cache-Control HTTP header to no-cache. This will cause the browser to always issue the request to the server. IE and every other browser treat HTTP requests originating from javascript like any other request.

Dave Mooney
  • 773
  • 8
  • 24
  • If I recall a long time ago, there were specific browsers that didn't respect this header, but I think this may no longer be the case. I can't think of any that still ignore Cache-Control – maple_shaft Nov 08 '11 at 20:48
1

If the query string is the same for your $.get() it's the same URL, the browser will return a cache, and rightfully so. If you want to ensure it always gets a fresh copy, append a bogus query string parameter with a timestamp to the URL in the $.get().

CaffGeek
  • 20,889
  • 16
  • 95
  • 176
  • This is an interesting idea. I hope to not have to do this across my entire application. I do not see the logic behind a GET request being valid to cache simply because the URL did not change. Query params allow powerful lookups and this excludes the possibility that a READ operation can change over time (or rather a defined period of time). – P.Brian.Mackey Nov 08 '11 at 18:57
  • 1
    On top of adding a random query string parameter to prevent caching, you can also look at adding a `Cache-Control` header. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html – maple_shaft Nov 08 '11 at 19:20