WKWebView - What configuration is needed to honour cache control headers?

Hi,


I am loading a resource from a remote server and rendering its content in a WKWebView instance. I am configuring URLRequest object with a cachePolicy of .useProtocolCachePolicy and using the load(request:) method on the webview instance.


The remote resource has a response header of Cache-Control: max-age=31536000. Yet the webview does not honour this cache policy. It always redownloads the remote resource.


What other configurations are needed to ensure the resource is cached and persisted to disk for future cached loading? I see WKWebView uses a WKWebsiteDataStore object to persist cache. Is there something I need to configure there to get it to cahce correctly?


Am I right in saying that WKWebView does not use the shared URLCache?


Thanks!

Replies

The html file successfully loaded from cache each time, both on startup and in app reloads.

Cool. This indicates that the behaviour you’re seeing is specific the web view. That’s kinda outside my area of expertise. You might have more luck asking about it over in WebKit Land™.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Will continue this discussion in WebKit Land 🙂, thanks for all your help to date.

I ended up implementing a WKURLSchemeHandler for browser requests such as js and css files as they need to be intercepted for authorization purposes. However, it looks like the browser waits syncronously for all the urlSchemeTasks to be completed before the browser renders the page. Do you know if a way to asyncronously load these resources?


For example. I download a html page and the html page has custom protocol references for js and css files. These are downloaded in the scheme handler using a URLSession client. But it looks like the browser waits for these files to be downloaded before the html page is rendered. Is that expected?

That sounds reasonable. The browser can't really the rest of the page without those resources. You can sometimes see this in Safari if you have a flaky server or poor connection. If you cancel the load, you can get a corrupted HTML page with no styling.


What I think you need here is a separation of responsibilities. WebKit is not flexible. It does what it does and the more unusual your code, the greater the risk. My suggestion would be to have a REST service for this data. Your app can request the resources and use a native interface for any authentication. The, once it has everything, it can render it in a web view.


Another option might be to serve intermediary placeholders. Once you get the real data, you can then issue a refresh with the real data.