Universal Windows 10 WebView - how to clear/disabl

2019-05-04 19:56发布

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.

4条回答
小情绪 Triste *
2楼-- · 2019-05-04 20:11

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

查看更多
虎瘦雄心在
3楼-- · 2019-05-04 20:18

I had a similar problem with css in an UWP app not getting cleared. I ran ProcessMon and found that the UWP app was caching .css and .js files in Windows 10 at: C:\Users\\AppData\Local\Packages\microsoft.windows.authhost.sso.p_8wekyb3d8bbwe\AC\INetCache\Q8IHZDMV. I'm using the Web Authentication Broker so the scenario might be slightly different. If a location similar to this doesn't pan-out, you might want to try to run processmon (part of the SysInternals suite) when you start-up your UWP app to see if you can identify the path the UWP app is using for caching these assets. Once there, deleting these assets and restarting the app did the trick.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-05-04 20:23

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);
        }
查看更多
成全新的幸福
5楼-- · 2019-05-04 20:29

Refreshing the webview seems to do a reload:

mainWebView.Refresh();

It's definitely a hack, but you may be able to insert at that some point in your application lifecycle to force reloading your content. Perhaps after the "mainWebView_NavigationCompleted()" event?

查看更多
登录 后发表回答