expo: onMessage/postMessage WebView Communication not work on Expo 32 but work on Expo 31 or below

I am using Expo to integrate webview. I use onMessage/postMessage WebView Communication for WebView Communication.

onMessage/postMessage WebView Communication not work on Expo 32 but work on Expo 31 or below.

import React, { Component } from 'react';
import { View, StyleSheet, WebView, Text , Alert} from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={{flex: 1, paddingTop: Constants.statusBarHeight}}>
        <WebViewAutoHeight />
      </View>
    );
  }
}

class WebViewAutoHeight extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      receivedMessageCount:0,
      receivedMessages: [],
    }
  }
  
  addMessage(message){
    Alert.alert("Something", "Something");
    this.setState({
      receivedMessages: [...this.state.receivedMessages, message]
    })
  }

  render() {
    return (
      <View style={{flex:1, width:'100%'}}>
        <Text style={{backgroundColor:'black', color:'white', fontSize:20}}>{this.state.receivedMessages.length} Messages from webview</Text>
        <Text style={{backgroundColor:'black', color:'white', fontSize:14}}>
          {this.state.receivedMessages.join('\n')}
        </Text>
        <WebView
          source={{html:`
            <html>
              <head>
                <script>
                  function test(){
                    window.postMessage("abc", '*')
                    alert("test");
                  }
                </script>
              </head>
              <body>
                <button onClick="test()">Test7</button>
              </body>
            </html>
          `}}
          scrollEnabled={false}
          onMessage={(evt)=>{
            this.addMessage(evt.nativeEvent.data);
          }}
          javaScriptEnabled={true}
        />

      </View>
    );
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 29 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Since nobody mentioned yet: Another workaround is to use useWebKit={true} option. If you choose to do that you might want to check this: https://facebook.github.io/react-native/blog/2018/08/27/wkwebview

@lgandecki besides downgrading expo package to 31.0.6, you should also set sdkVersion in your app.json config to 31.0.0, otherwise Expo Client will continue to use the native code for SDK32 which has this bug.

We also apologize that the fix is not available to use yet, but it happened for reasons beyond our control šŸ˜ž We’re really working hard to get it out asap.

Just submitted a new Expo Client build that fixes this issue for Apple review šŸ˜Ž An update will be available once they approve it.

Glad to see that the react-native-webview will be added to SDK33. As far as I see, I have 2 options for now:

  1. Downgrade to SDK31
  2. Eject and use react-native-webview

Just wanna be sure: is there any other solution rather than these two?

If not, I really don’t want to downgrade, but I’m wondering whether I can un-eject safely and continue to use react-native-webview with SDK33?

@schellack what steps do you take to ā€œdowngrade and reloadā€?

Is this released somewhere, or is there a way to use this fix? I wasted a few very frustrating hours today trying to understand what am I doing wrong… 😦 I tried the 32.0.1 but it doesn’t seem to be fixed. I also tried the 31.0.6 and 31.0.0 and they also produce the same result. I changed the expo version and the react-native sdk. to sdk-31. Not sure if this is beacuse of some cache. I removed and reinstalled node_modules, and run expo with expo start -c. I Erased all content and settings on the simulator, and this is still happening.

Don’t want to complain - I understand that this is a free and open source project and I appreciate that. I maintain a few of those by myself. Nonetheless, I would feel bad leaving a regression that brakes applications for weeks on end, especially if the fix is known and merged…

I have the same problem, though only on iOS. Here is a simple snack you can also use as another example: https://snack.expo.io/@salus/posting-from-webview-to-rn

Note that in my example, I am calling an external url as the WebView source. This is to show that the web server is returning a 404, because the window.postMessage call is not posting back to the web view (or the web view is not accepting it properly). Instead, the web view is attempting to navigate to ReactABI32_0_0-js-navigation://postMessage. That’s less apparent when running inline html in the web view.

If you downgrade the snack to sdk version 31 and reload the app, the problem goes away.

…the roadmap…

For anyone who may be interested: https://github.com/expo/expo/milestone/22

Sorry @lgandecki for not responding. Yes, we’re still struggling with their review, so I’m afraid that if you want this bug fixed in Expo Client you would have to downgrade to SDK31 šŸ˜” Anyway, the fix for this has been deployed to turtles, so standalone apps should work as expected - I hope that’s enough for you šŸ™‚

Any ideas when this will be published? I just realized that the expo 31 uses the old version of webview which causes iOS really unusable (half a second lag after a click, among other things). I’ve actually rearchitectured my app from ejected back to non-ejected once 32 was released (the only reason for ejection was to use a custom, performant, webview). I would really like to avoid migrating it again to ejected because of this bug, but I do need to release this in a week. 😦

Thank you for an immediate response, and on Sunday! šŸ’Æ Yes, app.json did the trick and the ā€œReactABI32_0_0-js-navigation://postMessageā€ issue disappeared.

The functionality still doesn’t work but I probably messed something up while trying to figure out what’s wrong, so it’s probably on my side.

thanks again!

Ok, fixed my problem, so I can confirm - works like a charm on 31 😃