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

我怕爱的太早我们不能终老 提交于 2019-12-07 09:44:23

问题


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.


回答1:


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);
        }



回答2:


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




回答3:


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?




回答4:


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.



来源:https://stackoverflow.com/questions/33391437/universal-windows-10-webview-how-to-clear-disable-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!