Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal Windows 10 WebView - how to clear/disable cache?

I am having a lot of trouble clearing the WebView cache in my UWP app.

If I edit the content of a JS file linked from my HTML page, I can't get the change into my app unless I re-install the app.

The static WebView.ClearTemporaryWebDataAsync() method doesn't seem to work.

I have also tried adding headers to the request to disable caching:

private void reloadPage()
{
    string url = getUrl();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
    request.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
    request.Headers.Add("Pragma", "no-cache");
    myWebView.NavigateWithHttpRequestMessage(request);
}

I also tried the following, on a punt (I'm not sure if this affects the WebView's caching behaviour), but still no joy:

private void onWebviewLoaded(object sender, RoutedEventArgs e)
{
    Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
    myFilter.CacheControl.WriteBehavior = Windows.Web.Http.Filters.HttpCacheWriteBehavior.NoCache;
    myFilter.CacheControl.ReadBehavior = Windows.Web.Http.Filters.HttpCacheReadBehavior.Default;

    WebView.ClearTemporaryWebDataAsync().AsTask().Wait();
    reloadPage();
}

Any help would be very much appreciated!

EDIT (14/12/15): I have found that adding headers to the request (as in the first code example above) does work, but only if this has been in place for the lifetime of the app, since install. Which makes sense, as it's just saying not to cache this particular request - it could still use an old cached version.

This works as a cludge for now, but it would be much nicer to be able to make use of caching (e.g. for the duration of an app session), then later clear the cache (e.g. on next startup).

EDIT (14/07/16): The above approach doesn't seem to bear out. Caching behaviour seems to be erratic in the webview...

On a clean install of the app, I can see changes to CSS/JS files with absolutely NO code to clear/disable cache. Then at some seemingly arbitrary point, files seem to be cached, and I cannot clear them from the cache.

like image 615
Jasongiss Avatar asked Sep 27 '22 09:09

Jasongiss


2 Answers

This finally works for me:

await WebView.ClearTemporaryWebDataAsync();
        Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
        var cookieManager = myFilter.CookieManager;

        HttpCookieCollection myCookieJar = cookieManager.GetCookies(new Uri("http://www.msftncsi.com/ncsi.txt"));
        foreach (HttpCookie cookie in myCookieJar)
        {
            cookieManager.DeleteCookie(cookie);
        }
like image 142
Jolly Roger Avatar answered Oct 11 '22 18:10

Jolly Roger


I don't know if it helps but try to add a timestamp behind the url (url + "?=" + sometimestamphere)

like image 41
Drast Avatar answered Oct 11 '22 16:10

Drast