Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Clearing WebView cache
-
Just wasted some time trying to understand why my WebView was not working. It seemed that the Javascript included on the HTML page (
<script src=...
) was not working properly.Turned out the included script file is just being cached, and I have to restart Pythonista to get the latest version. Calling
reload
made no difference.Looking on the net, it seems I might need to empty the shared URL cache. Could someone help me translate the following to
objc_util
?[[NSURLCache sharedURLCache] removeAllCachedResponses];
Or other options for ensuring that the latest script is loaded (without adding any random parts to the URL)?
Thanks,
Mikael -
Managed to convert the snippet above to:
NSURLCache = objc_util.ObjCClass('NSURLCache') url_cache = NSURLCache.sharedURLCache() url_cache.removeAllCachedResponses()
... which runs fine but does not fix the problem; I still need to restart Pythonista to get the latest version of a referenced Javascript file.
@omz, would you know of other options to clear the cache or get a fresh version of the file?
-
Try to get rid of the cache completely ...
from objc_util import ObjCClass NSURLCache = ObjCClass('NSURLCache') shared_cache = NSURLCache.alloc().initWithMemoryCapacity_diskCapacity_diskPath_(0, 0, None) NSURLCache.setSharedURLCache_(shared_cache)
... it helped me quite a while ago (not in Pythonista, elsewhere). Maybe it will work for you as well. One of the three hard things in computer science - caching, naming things and off-by-one errors :)
-
@zrzka, thanks. Unfortunately even that did not help. Maybe there is some caching hidden in the Pythonista version of the WebView?
-
Have you tried appending a time to the script query?
<script src="js/myfile.js?t=<?=time()?>" type="text/javascript"></script>
From what i read, this will defeat caching.
-
@JonB, that's sure a neat way to implement the traditional cache avoidance. I was unwilling to go that route, as there are several CSS and JS files involved, and caching is not an issue in production.
-
-
@omz, thanks. Just to check: could the failure of the shared cache-clearing solutions be related to the fact that WebView does not seem to be released when I end the UI execution? E.g. if I have a Javascript
interval()
callingalert()
, it will keep popping up messages even after I have closed the WebView. -
Have you tried having a close() methd that loads a new page (or an empty page)into the WebView?
-
@JonB, thanks, now I have :-)
Tried loading an empty page (load_html) and loading an unrelated page (load_url), to no effect except that the editor freezes for a moment as the page is loading in the background.
Also tried combining the load with cache-clearing action, but no joy.
-
Load empty (then pause) before cache clear?
Ok, how about dynamic modification of the page after load to alter all of the script tags to append a unique identifier (time)?