Skip to content

Commit

Permalink
feat(android): proxy service worker requests through local server (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkgroenen authored Apr 14, 2020
1 parent 7974eb4 commit c672175
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ Default value is `http`

Configures the Scheme the app uses to load the content.

#### ResolveServiceWorkerRequests

```xml
<preference name="ResolveServiceWorkerRequests" value="true" />
```

Default value is `false`

Enable to resolve requests made by Service Workers through the local server.

#### MixedContentMode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c672175

Please sign in to comment.