nonameShijian commented on issue #1693: URL: https://github.com/apache/cordova-android/issues/1693#issuecomment-1947997339
> Ok thank you, that confirms that it still doesn't work "out of the box". > > Glanceful reading, I'm guessing it's because the native needs to handle service worker registration using [ServiceWorkerClient](https://developer.android.com/reference/android/webkit/ServiceWorkerClient)[ServiceWorkerController], (https://developer.android.com/reference/android/webkit/ServiceWorkerController), and [ServiceWorkerWebSettings](https://developer.android.com/reference/android/webkit/ServiceWorkerWebSettings). > > Cordova doesn't currently do this. The good news is the above classes were added in API 24, which fits within our minimum API level, so if a PR can be crafted and tested then we could potentially roll it into our 13.x release. > > However, to re-iterate the service worker registration should probably be in the origin and context of webview asset loader (e.g. `https://localhost` / internal storage). In fact, MDN states that service workers must be ran in a secure context, thus it needs to go through the webview asset loader and the asset loader must be configured to use the https protocol. Even if the security implications are ignored, there might be technical limitations in registrating the service worker from external storage. I used 'ServiceWorkerController' in the construction method of the 'SystemWebViewClient. java' file, and then 'ServiceWorker' was successfully registered. Thank you again for your help. ```java this.assetLoader = assetLoaderBuilder.build(); ServiceWorkerController swController = ServiceWorkerController.getInstance(); swController.setServiceWorkerClient(new ServiceWorkerClient() { // I don't know how to reuse this variable, so I wrote it twice private final WebViewAssetLoader assetLoader = assetLoaderBuilder.build(); @Override public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { // Capture request here and generate response or allow pass-through return assetLoader.shouldInterceptRequest(request.getUrl()); } }); ServiceWorkerWebSettings serviceWorkerWebSettings = swController.getServiceWorkerWebSettings(); serviceWorkerWebSettings.setAllowContentAccess(true); serviceWorkerWebSettings.setAllowFileAccess(true); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
