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

window.ReactNativeWebView.postMessage is not a function #2680

Closed
jackkinsella opened this issue Sep 19, 2022 · 13 comments
Closed

window.ReactNativeWebView.postMessage is not a function #2680

jackkinsella opened this issue Sep 19, 2022 · 13 comments

Comments

@jackkinsella
Copy link

jackkinsella commented Sep 19, 2022

Bug description:

I'm getting a strange (and relatively rare) exception reported in Sentry.

window.ReactNativeWebView.postMessage is not a function

This exists within a piece of code that explicitly checks if window.ReactNativeWebView is present

if (window.ReactNativeWebView) {
  window.ReactNativeWebView.postMessage(message)
}

To Reproduce:

Because it happens rarely, I can't give exact reproduction steps.

However:

  • two instances were on a Pixel 4 (technically one of these was a Pixel 4a)
  • one instance was on an SM-G996U

This appears to be sporadic, even for any given user. I.e. they will experience the bug briefly then it won't happen again.

Environment:

  • OS: Android
  • OS version: "Other" (that's all Sentry gave us)
  • react-native version: 0.68.2
  • react-native-webview version: v11.22.1

Remarks

  • Is it possible that window.ReactNativeWebView needs time to load its properties?
  • Is it possible that window.ReactNativeWebView behaves differently depending on the OS/environment the code finds itself executed in? It seems like postMessage is a value instead of a function here.
@TheAlmightyBob
Copy link
Collaborator

It seems like postMessage is a value instead of a function here

You would also get that error if postMessage doesn't exist on ReactNativeWebView. Unfortunately I have no idea how that would be possible... seems like maybe a quirk/bug of the Android WebView...

@kassemitani
Copy link

We have the same the issue, reported by Bugsnag for Android devices.

@17Amir17
Copy link
Contributor

17Amir17 commented Oct 19, 2022

Same here, seems to happen when page takes long time/fails to load

@17Amir17
Copy link
Contributor

Can we get this fixed? I'm still seeing this

@TheAlmightyBob
Copy link
Collaborator

@17Amir17 Based on the limited information thus far, this sounds like an issue with Android's WebView that this library can't do anything about. I could be wrong, but we don't have a repro and the info from @jackkinsella indicates that we are successfully invoking addJavascriptInterface.

I would be somewhat curious if v11.21.2 behave[s|d] any differently, since there was some refactoring in v11.22.0... I would be surprised if that actually makes a difference though, as I don't think the refactoring would be related, but there were one or two things that unexpectedly broke there.

@17Amir17
Copy link
Contributor

17Amir17 commented Dec 23, 2022

This happened to me on v11.18.1 and also after upgrading to v11.26.0
I'll update here if I find a way to reproduce, seems to just happen randomly
Screenshot 2022-12-23 at 12 52 43

Also if it helps, the error persists even after calling webviewRef.current.reload() and only goes away after closing and opening the app

@antiline
Copy link

I have same issue on 11.25.1.

@mustvlad
Copy link

We have Expo 47 and "react-native-webview": "11.23.1" in our project and we constantly receive errors on Sentry that no messages were received from the webview. Strange enough, I couldn't find this error in the web app but it seems like it's happening anyway.

From our estimation this happens to about 1% of users on Android. In our case the user can't even login in the app if the webview connection is not working well, so we constantly receive 1* reviews on the store.

Anyone here found a fix?

Seems like the issue happens across a wide range of devices and android versions so I couldn't tell if there's some special condition.

@adambeer
Copy link

adambeer commented Feb 9, 2023

Any updates on this? Im using version 11.626.0 and im using a ref for the viewer. webviewRef.current.postMessage(); is not working on Android. I also tried with injectedJavascript with no luck:

(function() { window.postMessage = function(data) { window.ReactNativeWebView.postMessage(data); }; })();

@TheAlmightyBob
Copy link
Collaborator

TheAlmightyBob commented Feb 9, 2023

@adambeer webviewRef.current.postMessage() is different from the window.ReactNativeWebView.postMessage that this issue is about. Also, this issue is a very rare occurrence (which is why the problem/fix is unknown). If you're seeing a consistent failure, then that's something else.

@denissb
Copy link

denissb commented Feb 24, 2023

I think 90% of the problem in this case is actually making a reproducible example which I tried to do today and failed.
But this makes using the window.ReactNativeWebView.postMessage scary for mission critical functionality.

@github-actions
Copy link

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

@0xc22b
Copy link

0xc22b commented Aug 18, 2023

Can it be about the WebView process being killed by the OS on Android, i.e., running out of memory or inactive for a period?

It seems that after onRenderProcessGone is called, postMessage will be missing. And reload() doesn't help. Changing the WebView's key prop if onRenderProcessGone is called seems to fix the issue.

When the WebView process is killed and recreated, is something missing causing postMessage not a function?

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

9 participants