mercredi 22 septembre 2021

Delete cache of specific XHR call

I have a XMLHttpRequest that does a GET request:

const xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "myapi/getThing", true)

Normally, the response stays the same so the server instructs to cache it with a HTTP response header:

Cache-Control: public, max-age=604800, immutable

However, now I perform an action that I know for a fact will change the response, and so I call a XMLHttpRequest again, but the result won't change because of caching.

How do I make it so the next XHR call of this resource won't use the browser cache?

I have tried setting the Cache-Control to no-cache on the request:

xmlHttp.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0")

But the browser still uses the cache.

The goal is to explicitly NOT change the caching policy the server sets for performance reasons, so no validation and such. This should purely be the client wanting a 'fresh resource' on demand.




Aucun commentaire:

Enregistrer un commentaire