From c672175b7527d64b077f7715b2ff145325524add Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Tue, 14 Apr 2020 17:54:38 +0200 Subject: [PATCH] feat(android): proxy service worker requests through local server (#452) --- README.md | 9 +++++++++ .../cordova/webview/IonicWebViewEngine.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 6f387815..b9bf625f 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,15 @@ Default value is `http` Configures the Scheme the app uses to load the content. +#### ResolveServiceWorkerRequests + +```xml + +``` + +Default value is `false` + +Enable to resolve requests made by Service Workers through the local server. #### MixedContentMode diff --git a/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java b/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java index 81d04978..02bdaf8c 100644 --- a/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java +++ b/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java @@ -10,6 +10,8 @@ import android.os.Build; import android.support.annotation.RequiresApi; import android.util.Log; +import android.webkit.ServiceWorkerController; +import android.webkit.ServiceWorkerClient; import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; @@ -80,6 +82,19 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C if (!isDeployDisabled() && !isNewBinary() && path != null && !path.isEmpty()) { setServerBasePath(path); } + + boolean setAsServiceWorkerClient = preferences.getBoolean("ResolveServiceWorkerRequests", false); + ServiceWorkerController controller = null; + + if (setAsServiceWorkerClient && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + controller = ServiceWorkerController.getInstance(); + controller.setServiceWorkerClient(new ServiceWorkerClient(){ + @Override + public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { + return localServer.shouldInterceptRequest(request.getUrl(), request); + } + }); + } } private boolean isNewBinary() {