Strange reload behavior, WKWebView reloads with returnCacheDataElseLoad after background

Hi, in our app we have a WKWebView with complex web content that is loaded with cachePolicy: .reloadIgnoringLocalCacheData. If the app is in the background for several hours and returns in the foreground we noticed that the system reloads the webpage, but it does so with a cachePoliy returnCacheDataElseLoad. This could break the app if older cache content is present.

To reproduce start an app in the simulator (tested with iOS 17.2), put it in the background and via activity monitor stop the processes

  • com.apple.Webkit.WebContent
  • com.apple.Webkit.networking

After foreground the reload will happen.

Two questions:

  1. why is this reload happening after some hours in the background? We haven't seen any crash reports related to this. It mostly happens on one of our test devices (iphone13 with iOS17.2.1).
  2. why is the webview reloading with a modified cachePolicy (returnCacheDataElseLoad)?

Our temporary fix is to detect this case in "webView:decidePolicyFor navigationAction decisionHandler", cancel the request and reload with modified cachePolicy.

Any ideas?

Thanks, Heiko

Sample code:

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let config = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: config)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view.addSubview(webView)
        let myRequest = URLRequest(url: URL(string: "https://www.apple.com")!, cachePolicy: .reloadIgnoringLocalCacheData)
        print("request \(myRequest)")
        webView.load(myRequest)
    }
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print("start loading \(String(describing: navigationAction.request.url)), cache:\(navigationAction.request.cachePolicy)")
        decisionHandler(.allow)
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let fullscreen = CGRect.init(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
        webView.frame = fullscreen
    }
}

Submitted a bug-report regarding the changed cachePolicy: FB13567669

@phidev - we're experiencing this exact thing (thanks for the post!) ... is there any way we can see any updates on the bug report?

These days, you appear to only be able to see your own bug reports, so we can't view FB13567669 ... 🤦‍♂️

Strange reload behavior, WKWebView reloads with returnCacheDataElseLoad after background
 
 
Q