Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to catch blob download #1790

Closed
stefanchiriac opened this issue Dec 13, 2020 · 2 comments
Closed

Unable to catch blob download #1790

stefanchiriac opened this issue Dec 13, 2020 · 2 comments

Comments

@stefanchiriac
Copy link

I'm trying to catch blob url downloads to handle them manually since android is not supporting them and crashes the app, but I can't find a way to do it.
onShouldStartLoadWithRequest seems like is being triggered only when the react app loads, but after that no longer triggers.
The code below just prints http://10.0.2.2:9000/, but never blob

 const onShouldStartLoadWithRequest = (navigator: any) => {
    const {url} = navigator;
    console.log(url);
    if (url.includes('blob:')) {
      console.log('blob');
      webview.stopLoading();
      return false;
    }
    return true;
  };

  return (
    <WebView
      ref={(ref: any) => setWebView(ref)}
      originWhitelist={['*']}
      source={{uri: 'http://10.0.2.2:9000/'}}
      style={{}}
      allowFileAccess={true}
      allowFileAccessFromFileURLs={true}
      allowUniversalAccessFromFileURLs={true}
      javaScriptEnabled={true}
      domStorageEnabled={true}
      mixedContentMode={'always'}
      onFileDownload={onShouldStartLoadWithRequest}
      onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
      onNavigationStateChange={onShouldStartLoadWithRequest}
    />
  );

Environment:

  • OS: Android Embeded api 29
  • react-native version: 0.63.3
  • react-native-webview version: 11.0.2
@worstpractice
Copy link

I don't know about handling blob URLs on Android, but in order to (at the very least) not crash when navigating to blob URLs, I used the following snippet in the past:

const SimplifiedExample = () => {
  const webViewRef = useRef<WebView>(null);

  const handleNavigationStateChange = ({ url }: WebViewNavigation) => {
    if (!url) return;

    if (url.startsWith("blob")) {
      webViewRef.current?.stopLoading();
      console.log("Prevented blob url from loading");
    }
  };

  return (
  <WebView
    allowFileAccess
    allowFileAccessFromFileURLs
    allowUniversalAccessFromFileURLs
    onNavigationStateChange={handleNavigationStateChange}
    ref={webViewRef}
  />
  );
};

If it still crashes, I seem to recall that not showing a loading spinner/rendering an error screen helped.

Hope this helps.

@stefanchiriac
Copy link
Author

stefanchiriac commented Jan 14, 2021

Is still crashing with your code, even without loading spinner. But thanks anyway for the solution.

For now I'm just using #1817

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants