Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView with service worker (what are ServiceWorkerController and ServiceWorkerWebSettings?)

I have a service worker going fine in Chrome desktop and Chrome mobile, and I can tell exactly what is happening via the amazing DevTools in Chrome desktop (monitoring Chrome mobile remotely via USB). My service worker is based very closely on this example. The page reloads fine without a network connection, and from the Network tab of DevTools I can confirm that resources are being cached and served by the service worker as expected.

But I am also loading the same page in a WebView and I'm struggling to determine whether the service worker is actually registering properly and operating as it should. I can't see a way of debugging this in the same way as you can with DevTools in Chrome desktop/mobile.

What is certain is that the page doesn't reload without a connection when it is in the WebView, so some resources aren't being cached. This is despite the fact that service workers seem to be supported in WebView.

Looking around the place for possible steps or settings I might be missing to get it working as it does in Chrome, I came across ServiceWorkerController and ServiceWorkerWebSettings, but cannot find any documentation or examples for these (other than the basic docs linked). A search on StackOverflow for these terms gives zero results.

Is there a working example somewhere of a WebView that loads a page with attached service workers? Any idea how to use the above two classes... are they actually necessary?

like image 495
drmrbrewer Avatar asked Aug 19 '17 15:08

drmrbrewer


People also ask

Does WebView support service workers?

As per this announcement, service workers have been supported in Chrome WebViews since March of 2015 (Android WebView v40+). Devices running Android OS 5.0 and higher should have it.

What is a web service worker?

Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.

What is System WebView licenses?

Android System WebView is a system component that lets Android apps display web content inside them without opening a dedicated browser. In other words, Android System WebView is a web browser engine or an embedded web browser dedicated solely for apps to show web content.

What is WebView used for?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.


1 Answers

This is only a partial answer, but hope it helps some anyways:

From what I can tell, the ServiceWorkerController is the Java implementation of ServiceWorker for WebView, which is separate from the Javascript implementation. The two do not seem to be tied together in any way.

The Java implementation is a singleton which persists and affects all WebViews in an application, and its only method is to intercept requests via shouldInterceptRequest. It is very similar to WebViewClient, the latter of which has more functionality and only affects the WebViews it is attached to (via setWebViewClient).

The link you posted does seem to indicate that WebViews do support the Javascript version of ServiceWorker. Did you by any chance miss enabling Javascript through WebSettings.setJavascriptEnabled? Also, ensure that you are targeting minimum SDK version 21 as ServiceWorkers are only enabled in Android 5.0 and higher. I am interested to know if you or anyone has managed to get a Javascript ServiceWorker functioning inside a WebView as well.

like image 156
Jeff Reznik Avatar answered Sep 17 '22 20:09

Jeff Reznik