1
votes

There are a lot of answers out there for browser caching but most are about either turning caching on or off or requesting using a different url for some static asset like js or images.

  • I am loading json data over an ajax request
  • This is dynamic content, say a list of tasks
  • I only want to update when a user has done a certain action. Until then it can be cached forever.
  • When the user takes that action, I want to invalidate the cache and get a new version from the server.

    1. Is there a way to clear the browsers local cache without changing the url. So, /tasks is cached until next year. I want to invalidate that and force future requests to /tasks to get new data and a new expires header.

    2. Would I be better off not allowing browser caching and cache the requests/data on the server instead?

Another similar question: Forcing AJAX request to revalidate cache with server, without reloading completely

1
Isn't it easier to use localStorage/Sessionstorage to store the data, and when the user does a action clear that store and get a new from the server?VeldMuijz
@VeldMuijz - Yea that is a great suggestion. I might be able to leverage that! Thanks. I would also like to know what people have to say about the two questions I have though for future referencestyks

1 Answers

-1
votes

foreach request with cache: false jquery will add a timestamp as a get paramater, so the request is unique and won't be cached

$.ajax({
    url:"example.php"
    cache:false
});

referring to the jquery docs: cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.