0

I have built a browser extension that, on startup, calls a web service from which it retrieves a JSON result. The JSON result contains data that will be shown in the popup of the extension. This data is updated at least once a day using a prepared, scheduled update service. Furthermore, the data is irregularly manually updated intra-day.

The data that is retrieved from the web service does not automatically adapt to the changes that are made. Particularly, the extension retrieves old results from the web service. The web service is tested and always returns the newest result, hence this is not a server-side problem (at least not with regards to the computation, storage and provision of the data).

I noticed that sometimes the web service appears to return old results, which then after a page refresh are updated. I believe this happens due to some caching mechanism(s) client- or server-side.

Researching the topic has not yielded any helpful resource or material

Is it possible for me to instruct the extension to not cache results from the web service and/or instruct the server to not allow/serve cached results?

Any advice and/or resource regarding this issue is very much appreciated.

user7259296
  • 101
  • 1
  • 5
  • Did my answer help you? If so, please accept it as answer, so anyone with the same questions knows there is a working solution. – user2154065 Dec 13 '16 at 07:00
  • Admittedly, I have not yet attempted to implement a solution according to your suggestions. As soon as I have, I will do as you request. Until then, thank you for your time and help and let's hope your suggestion is successful - it sounds very promising. – user7259296 Dec 13 '16 at 11:13

2 Answers2

1

One usual way would be to add a random extension to your HTTP GET path - a parameter, not used by the server. By doing this, the browser cannot use cached data, because it just has not got any for this URL. The server will simply ignore the additional parameter.

Here is an example, please change the value of the random parameter (12345 in this example) with each call to the server.

// Original URLs
http://www.example.com/myservice
http://www.example.com/myservice?param=data

// With an Additional HTTP Parameter
http://www.example.com/myservice?random=12345
http://www.example.com/myservice?param=data&random=12345
user2154065
  • 1,137
  • 2
  • 10
  • 23
1

If you have control over server software right thing to do is to configure it to set proper caching related HTTP header values. To prevent caching you need to set Cache-Control header value and Expires header value. Check this thread for a cross-browser solution. In order to not transfer to the clients data that is not stale yet again and again use either ETag or Last-Modified header values. For more information about ETag, conditional requests and HTTP caching in general check HTTP caching tutorial and HTTP protocol specification.

Community
  • 1
  • 1
Leonid Vasilev
  • 10,092
  • 3
  • 31
  • 44