How to avoid AJAX caching in Internet Explorer 11 when additional query string parameters or using POST are not an option

AjaxInternet ExplorerCaching

Ajax Problem Overview


I realize this question has been asked, but in modern REST practice none of the previous iterations of this question nor their answers are accurate or sufficient. A definitive answer to this question is needed.

The problem is well known, IE (even 11) caches AJAX requests, which is really really dumb. Everyone understands this.

What is not well understood is that none of the previous answers are sufficient. Every previous instance of this question on SO is marked as sufficiently answered by either:

  1. Using a unique query string parameter (such as a unix timestamp) on each request, so as to make each request URL unique, thereby preventing caching.

-- or --

  1. using POST instead of GET, as IE does not cache POST requests except in certain unique circumstances.

-- or --

  1. using 'cache-control' headers passed by the server.

IMO in many situations involving modern REST API practice, none of these answers are sufficient or practical. A REST API will have completely different handlers for POST and GET requests, with completely different behavior, so POST is typically not an appropriate or correct alternative to GET. As well, many APIs have strict validation around them, and for numerous reasons, will generate 500 or 400 errors when fed query string parameters that they aren't expecting. Lastly, often we are interfacing with 3rd-party or otherwise inflexible REST APIs where we do not have control over the headers provided by the server response, and adding cache control headers is not within our power.

So, the question is:

Is there really nothing that can be done on the client-side in this situation to prevent I.E. from caching the results of an AJAX GET request?

Ajax Solutions


Solution 1 - Ajax

Caching is normally controlled through setting headers on the content when it is returned by the server. If you're already doing that and IE is ignoring them and caching anyway, the only way to get around it would be to use one of the cache busting techniques mentioned in your question. In the case of an API, it would likely be better to make sure you are using proper cache headers before attempting any of the cache busting techniques.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ

Cache-control: no-cache
Cache-control: no-store
Pragma: no-cache
Expires: 0

Solution 2 - Ajax

If you don't control the API, you might be able to disable IE caching by adding request headers on the ajax gets:

'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstolliView Question on Stackoverflow
Solution 1 - AjaxKevin BView Answer on Stackoverflow
Solution 2 - AjaxMark MView Answer on Stackoverflow