简体   繁体   中英

(How) can I instruct Chrome/Firefox/IE/… not to cache a specific website?

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM