Search code examples
androidwebviewbrowser-cacheetag

Android WebView not using ETag for proper caching


I use a WebView in Android/Kotlin with

settings.cacheMode = LOAD_DEFAULT

The server does not return any Cache-Control: header! But it returns valid ETag headers for all requests.

What I want is, that the WebView is sending all requests with the If-None-Match: header set to only download files if it is really needed. If the tag matches, it should use the 304 header returned by the server.

But I find the WebView is caching all the time. At least for 10 minutes or more. Looks very random. And it does not seem to send the If-None-Match: header at all because the server never replies with 304 header.

If I disable the cache for the WebView (cacheMode = LOAD_NO_CACHE), it is also not using the ETag. Instead, it always loads everything (as expected).

How can I make the WebView use the Etag to handle caching? Short requests and using ETag would be perfect.

It is for a web app and it should only fully reload bigger ressources if they really have changed.


Solution

  • The short answer is, that Android WebView is not capable of using ETag headers for caching properly. It does some internal caching only.

    It is possible to implement the use of ETag and If-Modified-Since headers by using callbacks.

    I found a site that shows how this is done: https://medium.com/android-news/reducing-your-networking-footprint-with-okhttp-etags-and-if-modified-since-b598b8dd81a1

    In short words, you intercept the traffic and build your own caching system around your own ETag and If-Modified-Since headers.