Bug description:
As there is breaking change in 6.0.2 that we can not use window.postMessage() and window.__REACT_WEB_VIEW_BRIDGE.postMessage() anymore. To avoid replacing all instances with the window.ReactNativeWebView.postMessage(), i just injected the javascript like this:-
const injectedJavascript = (function() {
window.postMessage = function(data) {
window.ReactNativeWebView.postMessage(data);
};
window.__REACT_WEB_VIEW_BRIDGE = {};
window.__REACT_WEB_VIEW_BRIDGE.postMessage = function(data) {
window.ReactNativeWebView.postMessage(data);
};
})();;
However, the above code works perfectly when the app is running in development mode but fails in release mode for android.
<WebView
domStorageEnabled
javaScriptEnabled
automaticallyAdjustContentInsets
source={LocalWebURL}
scrollEnabled
mixedContentMode={'always'}
setJavaScriptEnabled
injectedJavaScript={loaded ? injectedJavascript : null}
mediaPlaybackRequiresUserAction={false}
onNavigationStateChange={this.onNavigationStateChange}
ref={webView => {
if (webView) this.webView = webView
}}
startInLoadingState
onError={this.handleError}
// cacheEnabled
// thirdPartyCookiesEnabled
onMessage={message => {
console.log('the url is', message)
this.onMessageRecieved(message.nativeEvent)
}}
allowsInlineMediaPlayback
onLoadEnd={() => {
this.webView.injectJavaScript(injectedJavascript)
if (this.state.isInitialRender) {
this.setState(
{ loaded: true, isInitialRender: false, error: false },
() => {
if (this.props.postJsonData) {
this.webView.postMessage(
JSON.stringify(this.props.postJsonData)
)
}
}
)
}
}}
/>
To Reproduce:
Run the application in release mode then the injected javascript doesn't work. Tried with injectedJavascript and injectJavascript api.
Expected behavior:
InjectedJavascript should work in release mode also.
Environment:
@jamonholmgren @Titozzz Please help me with this i'm stuck.
Injected javascript is working in release mode as I've been using it for more than 2 years, so the problem is somewhere else. Your injected code looks ok however so I'm not sure what could be the issue. Please run in release mode and use https://github.com/react-native-community/react-native-webview/blob/master/docs/Debugging.md to understand what's going on
@Titozzz thanks for the quick reply. Yeah you are right issue is not with injectedJavascript it was onMessage which was not getting triggered when the debugger was off. If in case anyone stuck in such issue, I resolved it by removing console.log in onMessage callback. Once again thanks