0

I have a webapp which I am loading inside a webview of android.

The app consist some huge files of 1MBs which is not cached in webview so the time taking to load the files each time is little high. as an experiment I wanted to include those files within the asset folder of the app and load it from there using javascript fetch inside the webview.

But I ended up getting the cors exception like below

VM45:769 Fetch API cannot load `file:///android_asset/kixr_detector_001.wasm. URL scheme must be "http" or "https" for CORS request.`

Is there anyway to load a file which residing in android asset folder to an app running in webview ? or another way to cache these files.

Any help is much appreciated. Thanks in advance.

Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
rahul cp
  • 307
  • 1
  • 3
  • 15
  • https://stackoverflow.com/questions/56251951/webassembly-webview-javascript-interface-for-heavy-calculations – exploitr Jul 11 '19 at 06:57

2 Answers2

0

From the comment mentioned here : Fetch API cannot load file:///C:/Users/Jack/Desktop/Books_H/book-site/public/api/books. URL scheme must be "http" or "https" for CORS request

Browsers don't allow file:// URLs with ajax calls (for security reasons). You need to load your web page through a web server and make your ajax request through that web server using http:// or https://, not file:// –


(From Comments)
You can try to enable caching in WebView by :

webViewComp.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webViewComp.getSettings().setAppCacheEnabled(true)
exploitr
  • 843
  • 1
  • 14
  • 27
  • i was loading these files from s3 but those files are not getting cached each time it downloading. as a solution to that only i tried to load from file:// is there any other solution. or can you please enlighten me why in web-view files are not getting cached – rahul cp Jul 11 '19 at 07:20
  • If it's working that way, then you can enable WebView caching from WebSettings. Updating the answer, wait. – exploitr Jul 11 '19 at 07:21
  • But, normally the WebView caches things. Anyway, check the updated answer – exploitr Jul 11 '19 at 07:27
  • we have enabled the setCacheMode but still each time it downloading fresh – rahul cp Jul 11 '19 at 11:16
  • Did you try `appCache` Too ? – exploitr Jul 11 '19 at 11:24
0

you can do enable your webview cache from webview settings below is code

webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT)       
webview.getSettings().setAppCacheEnabled(true); 
Sahil Arora
  • 495
  • 1
  • 3
  • 11