How to prevent remote JSON getting cached on some Android?


#1

Hallo
I wrote an Ionic1.x app which retrieve and display a remote JSON via $http.get
On the server, a new JSON gets published whenever backend data changes, so that the app can fetch directly a JSON rather than a webservice endpoint.
The app fetches a new JSON whenever the user enters a specific view (for the moment: I’m goin to implement a more reliable caching after tests).
The service definition:
app.service('HttpService', function($http, ApiEndpoint) { return { all: function() { return $http.get( ApiEndpoint.url + '/json/full.json') .then(function(response) { return response.data; }); } } });
In the controller:
app.controller('ProductsCtrl', function($scope, HttpService) { HttpService.all() .then(function(response) { $scope.products=response.products; }); })

It runs OK on emulators and some real devices (tried some Android 5.0.6, 6.x, 4.1.2, 4.4.2, iOS 9.3).
On an LG G3 running “Android 5.0”, the JSON doesn’t update until I clear app data from Android’ App Management.
Why the JSON gets cached? How could I prevent this?
Thanks in advance


#2

Try to use a cache busting approach like adding a timestamp to the url

return $http.get( ApiEndpoint.url + ‘/json/full.json?a=’ + Date.now())


#3

thank you! Indeed I was right now testing with:
return $http.get( ApiEndpoint.url + '/json/full.json?a=' + Math.random().toString(36).substring(2))


#4

Be aware your approach could end up generating the same number. But it is the right direction. Good luck.