-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Remove errors for detecting native postMessage #10941
Remove errors for detecting native postMessage #10941
Conversation
By analyzing the blame information on this pull request, we identified @caabernathy and @spicyj to be potential reviewers. |
This is better. Though why overlap and break pages that already use window.postMessage, when RN can use a different name? Say, window.rnPostMessage. Then this issue of namespace collision is sidestepped, and original webpages that uses window.postMessage can load on page without breakage. Otherwise, we could inject JS to run at precise the right moment (right after postMessage got moved to originalPostMessage), just to undo this RN behaviour by:
This would seem counter-productive at best, if not prone to breakage still as the above would need to be run at precisely the right moment after RN's replacement of window.postMessage? Original page's code would break if it calls window.postMessage that's RN's replacement instead of the real original. |
I'm not sure I see the benefit, since We could namespace this though: |
You sure postMessage does nothing by default? I was testing with Google I'm for namespacing just to avoid any kind of future possible headache with
|
Also I think it was used in some polyfill for How about revisiting the decision, and always require a window.postMessage = function(message, targetOrigin, transfer) {
if (targetOrigin !== 'react') {
window.originalPostMessage(message, targetOrigin, transfer);
}
bridge.postMessage(message);
}
Many existing third party websites use We could avoid breaking existing pages. It's just the current implementation. |
If adding targetOrigin allows differentiating iframe and RN calls of postMessage in all cases, I'm for it. |
I don't think allowing a targetOrigin of 'react' is spec compliant. I'd vote to namespace it and make it behave like postMessage api in node's child_process module, which does not use the targetOrigin parameter. |
making it work without a targetOrigin is not spec compliant either. it doesn't matter, it's a different environment and sometimes we need to extend APIs to make it work better. if you don't like 'react', then let's come up with a better one, say
what about the use case where I'd like to receive messages from an existing website via post message? this will add a new API with almost the same behaviour, which doesn't sound great. |
Just checked, the web worker spec has a postMessage method without a targetOrigin. |
it's |
We could expose a worker-instace-like object— |
How about:
@satya164 Would every user have to add this code to their webpage that's displayed in the WebView? @fungilation said:
I like this idea - very simple. Just call Maybe I'm missing something here? :) |
It's nice if you can make it spec-compliant but the spec probably didn't think about the use case of a WebView inside a React Native app so I don't mind.
The React Native app is not a worker though. No need to make this look like something from the spec when they're not related. |
To me it looks like this PR removes detection of an important bad behavior. As @fungilation said:
We should not silently ignore this. |
Based on the last comment I'll close this PR but please feel free to send a new PR for the |
Sorry, I meant close the other pr. I was going to finish this once we had settled on a direction. |
No. This can be injected automatically.
The reason we initially decided to use I don't see why it's an issue here to introduce a non-standard origin property. It's not spec compliant, but again the spec certainly didn't consider environments like RN.
Isn't it that we're replacing value, not pages, that's why there's the warning? I wasn't aware that the native |
How about this, type Message = {
target: ?string;
origin: string;
data: string;
}
The |
We’ll need to put the |
As I originally objected to this charade of renaming/calling If that is ensured, I have no objection. Otherwise, I just feel it's error prone. Especially if a webpage decides to override window.postMessage at an undetermined time later in an unpredictable way for whatever reason. Letting existing web JS code to call window.postMessage when placed inside RN is a rather edge case for this trouble. If people want to run existing code calling window.postMessage inside RN, a global search for If webpage's original window.postMessage isn't broken, don't fix it? (or rather, don't tamper with it) |
+1 to |
postMessage is broken for nearly every usecase, see #10865 |
Because the devs here are still stuck up in their ivory tower debate, and
taking a nap.
On Tue, May 16, 2017 at 11:30 PM axten ***@***.***> wrote:
postMessage is broken for nearly every usecase, see #10865
<#10865>
why not simply fixing this bug?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#10941 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADoJSidzIuy1inqXHvcT1iQoAdpflw6Sks5r6pP0gaJpZM4KyUWf>
.
--
Cheers,
Gary
|
I gave up on any updates on this and made a post-yarn shell script that patches native code: Works great so far! |
While I'm "stuck" with https://github.com/alinz/react-native-webview-bridge (patched for RN 0.40+), still. |
WKWebview in iOS namespaces their For example, this breaks websites that use From practical standpoint, the use-case here for RN apps is to communicate between webview and RN, in which case passing messages via existing browser api for I hope RN devs can consider to add a new API e.g. References:http://www.joshuakehn.com/2014/10/29/using-javascript-with-wkwebview-in-ios-8.html |
I think we have to reconsider this. |
@shergin, quite an understatement! 😂 But thank you. I want to repeat that this in its current form in RN is unusable (to me), and I'm resorting to use a mostly unmaintained and monkey patched library like https://github.com/alinz/react-native-webview-bridge for 2 way communication/RPC between RN and webview. |
Mmm... Interesting. How does that library work? I am starting believe that what we have to do it:
So, this particular solution probably bit obsolete. Oops. |
Agreed that pulling something like https://github.com/CRAlpha/react-native-wkwebview into RN core would be killing 2 birds/bugs with 1 stone. |
react-native-wkwebview doesn't address Android. |
New |
So, my vision of this problem is:
I don't know much about Android, unfortunately, but I hope:
Anyways, using I have not investigated this probmem deeply enough though, so I can be completely wrong. 😄 |
I don't know about the android side as I'm working solely with iOS so far with my RN app. But https://github.com/alinz/react-native-webview-bridge have long estabalished 2 way RN <> webview communication on both iOS (UIWebView) and Android so ideally in terms of expertise:
|
The current webview on Android is fine, you can use |
|
WKWebView has dedicated and very simple |
Comments from users on Issue #11594 also mention that this method is not available for Android release, only for debug. |
@twairball that is apparently caused by proguard. I am using it in release just fine. |
TL;DR Fixing the Problem should be a lot easier. Printing String(Object.hasOwnProperty) in a browser console prints out the following: React Native on iOS tries to check that basically no one has touched the window.postMessage function. This piece of code runs in the UIWebView's This is okay when you actually navigate to a new website, but is not the case when loading an iframe for example, because every iframe redirection will invoke this process again, meaning that the above code will run again creating this issue. The quick fix in this case, is to check if This is how I solved it locally, it may not be the best solution, but It doesn't break the current logic |
A lot of people are complaining that they are getting errors when using
postMessage
in WebViews. This is because pages are replacing this value. There isn’t a way that we can say they want to ignore the error, but it seems that in every case, they did want to ignore the error, and did not find it useful.This PR removes the checks, and documents
window.originalPostMessage
. If the user does care about this, they can inject JS into the webview to change it back (i.e.window.postMessage = window.originalPostMessage
).