Skip to content

Commit c672175

Browse files
authored
feat(android): proxy service worker requests through local server (#452)
1 parent 7974eb4 commit c672175

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ Default value is `http`
7272

7373
Configures the Scheme the app uses to load the content.
7474

75+
#### ResolveServiceWorkerRequests
76+
77+
```xml
78+
<preference name="ResolveServiceWorkerRequests" value="true" />
79+
```
80+
81+
Default value is `false`
82+
83+
Enable to resolve requests made by Service Workers through the local server.
7584

7685
#### MixedContentMode
7786

src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import android.os.Build;
1111
import android.support.annotation.RequiresApi;
1212
import android.util.Log;
13+
import android.webkit.ServiceWorkerController;
14+
import android.webkit.ServiceWorkerClient;
1315
import android.webkit.WebResourceRequest;
1416
import android.webkit.WebResourceResponse;
1517
import android.webkit.WebSettings;
@@ -80,6 +82,19 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C
8082
if (!isDeployDisabled() && !isNewBinary() && path != null && !path.isEmpty()) {
8183
setServerBasePath(path);
8284
}
85+
86+
boolean setAsServiceWorkerClient = preferences.getBoolean("ResolveServiceWorkerRequests", false);
87+
ServiceWorkerController controller = null;
88+
89+
if (setAsServiceWorkerClient && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
90+
controller = ServiceWorkerController.getInstance();
91+
controller.setServiceWorkerClient(new ServiceWorkerClient(){
92+
@Override
93+
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
94+
return localServer.shouldInterceptRequest(request.getUrl(), request);
95+
}
96+
});
97+
}
8398
}
8499

85100
private boolean isNewBinary() {

0 commit comments

Comments
 (0)