Skip to content
16 changes: 16 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.webkit.ServiceWorkerClient;
import android.webkit.ServiceWorkerController;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.activity.result.ActivityResultCallback;
Expand Down Expand Up @@ -274,6 +278,18 @@ private void loadWebView() {
webView.setWebChromeClient(new BridgeWebChromeClient(this));
webView.setWebViewClient(this.webViewClient);

if (Build.VERSION.SDK_INT >= 24 && config.isResolveServiceWorkerRequests()) {
ServiceWorkerController swController = ServiceWorkerController.getInstance();
swController.setServiceWorkerClient(
new ServiceWorkerClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
return getLocalServer().shouldInterceptRequest(request);
}
}
);
}

if (!isDeployDisabled() && !isNewBinary()) {
SharedPreferences prefs = getContext()
.getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
Expand Down
13 changes: 13 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class CapConfig {
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;
private String errorPath;
private boolean zoomableWebView = false;
private boolean resolveServiceWorkerRequests = true;

// Embedded
private String startPath;
Expand Down Expand Up @@ -179,6 +180,7 @@ private CapConfig(Builder builder) {
this.minHuaweiWebViewVersion = builder.minHuaweiWebViewVersion;
this.errorPath = builder.errorPath;
this.zoomableWebView = builder.zoomableWebView;
this.resolveServiceWorkerRequests = builder.resolveServiceWorkerRequests;

// Embedded
this.startPath = builder.startPath;
Expand Down Expand Up @@ -282,6 +284,7 @@ private void deserializeConfig(@Nullable Context context) {
useLegacyBridge = JSONUtils.getBoolean(configJSON, "android.useLegacyBridge", useLegacyBridge);
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);
zoomableWebView = JSONUtils.getBoolean(configJSON, "android.zoomEnabled", JSONUtils.getBoolean(configJSON, "zoomEnabled", false));
resolveServiceWorkerRequests = JSONUtils.getBoolean(configJSON, "android.resolveServiceWorkerRequests", true);

String logBehavior = JSONUtils.getString(
configJSON,
Expand Down Expand Up @@ -374,6 +377,10 @@ public boolean isInputCaptured() {
return captureInput;
}

public boolean isResolveServiceWorkerRequests() {
return resolveServiceWorkerRequests;
}

public boolean isWebContentsDebuggingEnabled() {
return webContentsDebuggingEnabled;
}
Expand Down Expand Up @@ -573,6 +580,7 @@ public static class Builder {
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;
private boolean zoomableWebView = false;
private boolean resolveServiceWorkerRequests = true;

// Embedded
private String startPath = null;
Expand Down Expand Up @@ -672,6 +680,11 @@ public Builder setUseLegacyBridge(boolean useLegacyBridge) {
return this;
}

public Builder setResolveServiceWorkerRequests(boolean resolveServiceWorkerRequests) {
this.resolveServiceWorkerRequests = resolveServiceWorkerRequests;
return this;
}

public Builder setWebContentsDebuggingEnabled(boolean webContentsDebuggingEnabled) {
this.webContentsDebuggingEnabled = webContentsDebuggingEnabled;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void setup() {
.setBackgroundColor("red")
.setPluginsConfiguration(pluginConfig)
.setServerUrl("http://www.google.com")
.setResolveServiceWorkerRequests(false)
.create();
} catch (Exception e) {
fail();
Expand All @@ -73,6 +74,7 @@ public void getCoreConfigValues() {
assertTrue(config.isWebContentsDebuggingEnabled());
assertEquals("red", config.getBackgroundColor());
assertEquals("http://www.google.com", config.getServerUrl());
assertFalse(config.isResolveServiceWorkerRequests());
}

@Test
Expand Down
9 changes: 9 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ export interface CapacitorConfig {
* @default false
*/
useLegacyBridge?: boolean;

/**
* Make service worker requests go through Capacitor bridge.
* Set it to false to use your own handling.
*
* @since 7.0.0
* @default true
*/
resolveServiceWorkerRequests?: boolean;
};

ios?: {
Expand Down
Loading