nonameShijian opened a new issue, #1693:
URL: https://github.com/apache/cordova-android/issues/1693

   # Bug Report
   
   ## Problem
   When I use ServiceWorker, the browser gives an error:
   `TypeError: Failed to register a ServiceWorker for scope 
('https://localhost/') with script ('https://localhost/service-worker.js'): An 
unknown error occurred when fetching the script.`
   
   ### What is expected to happen?
   The browser successfully registered ServiceWorker
   
   
   ### What does actually happen?
   The browser gives an error:
   TypeError: Failed to register a ServiceWorker for scope 
('https://localhost/') with script ('https://localhost/service-worker.js'): An 
unknown error occurred when fetching the script.
   
   
   ## Information
   <!-- Include all relevant information that might help understand and 
reproduce the problem -->
   I used Cordova version 10.0 to create an offline web game, which provides 
the WebViewAssetLoader class to access local web pages using HTTPS URLs. 
   But for the convenience of users to replace files, I want this webpage to 
display files stored in the external directory of the phone. The directory 
address is equivalent to 'cordova.file.externalApplicationStorageDirectory', so 
I made some modifications to the source code of CordovaLib. 
   The webpage display was successful, but the registration of ServiceWorker 
failed. After debugging, I found that the shouldInterceptRequest method of the 
SystemWebViewClient class cannot intercept the registration of ServiceWorker. 
Perhaps this is the reason for the registration failure.
   
   
   ### Command or Code
   <!-- What command or code is needed to reproduce the problem? -->
   Modify 'java/org/apache/cordova/engine/SystemWebViewClient.java' in the 
CordovaLib folder as follows:
   ```java
   // Replace this sentence with all the following code
   // InputStream is = 
parentEngine.webView.getContext().getAssets().open("www/" + path, 
AssetManager.ACCESS_STREAMING);
   
   AssetManager assetManager =  parentEngine.webView.getContext().getAssets();
   InputStream is;
   // The path originally set by Cordova
   String[] split = ("www/" + path).split("/");
   // Get the folder where the original path is located
   String[] newSplit = Arrays.copyOfRange(split, 0, split.length - 1);
   // All files and folders in the folder where the original path is located
   List<String> list = Arrays.asList(assetManager.list(String.join("/", 
newSplit)));
   if (list.contains(split[split.length - 1])) {
           is = assetManager.open("www/" + path, AssetManager.ACCESS_STREAMING);
   } else {
           File file = new File(
                   
parentEngine.webView.getContext().getExternalFilesDir(null).getParentFile(),
                   path
           );
           is = new FileInputStream(file);
   }
   ```
   
   
   ### Environment, Platform, Device
   <!-- In what environment, on what platform or on which device are you 
experiencing the issue? -->
   Android 12, and Google Webview 118
   
   
   ### Version information
   <!--
   What are relevant versions you are using?
   For example:
   Cordova: Cordova CLI, Cordova Platforms, Cordova Plugins
   Other Frameworks: Ionic Framework and CLI version
   Operating System, Android Studio, Xcode etc.
   -->
   Cordova 10.0.0,Android Studio
   
   
   ## Checklist
   <!-- Please check the boxes by putting an x in the [ ] like so: [x] -->
   
   - [x] I searched for existing GitHub issues
   - [ ] I updated all Cordova tooling to most recent version
   - [x] I included all the necessary information above
   


-- 
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: issues-unsubscr...@cordova.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to