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

Iframe + postMessage sometimes fails #2267

Closed
sz5000 opened this issue Mar 14, 2022 · 3 comments
Closed

Iframe + postMessage sometimes fails #2267

sz5000 opened this issue Mar 14, 2022 · 3 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@sz5000
Copy link

sz5000 commented Mar 14, 2022

We have a problem with PostWebMessageAsJson to an Iframe.

We use ICoreWebView2Frame2::add_WebMessageReceived to receive messages from an iframe.
The iframe sends commands to the host app via window.chrome.webview.postMessage.
The host app processes the commands and sends the result back to the iframe via ICoreWebView2Frame2::PostWebMessageAsJson.
Within the iframe we use window.chrome.webview.addEventListener('message', ... to receive the response.

This works most of the time. But if we change the source of the iframe to load a new version for a new command, then receiving the responses often does not work. I say often because sometimes it works and sometimes it does not work.

I've tested a bit around and I tried to send the command in an interval until I receive a response. This works sometimes and after a few seconds and retries I get the response. This is really weird and currently I do not see a reason for this and I also can not debug this because I do not have access to the internal code to see what happens here.

The host app always receives the message from the iframe. That part is OK. But the iframe does not always receives the reply from the host app, but the the host app sends the message back. Sometimes it works, but most of the time not.

Then I tried to use ICoreWebView2Frame2::ExecuteScript right after ICoreWebView2Frame2::PostWebMessageAsJson just to figure out whether a test script can be executed in the iframe. ICoreWebView2Frame2::ExecuteScript always works even if the prior call to ICoreWebView2Frame2::PostWebMessageAsJson fails to receive the message.

Response of ICoreWebView2Frame2::PostWebMessageAsJson is always S_OK, even if the message does not receive the iframe.

The question now is why this is so. How can I debug this case to find out the problem?

Version
SDK: 1.0.1150.38
Runtime: WebView2-x64-99.0.1150.39
Framework: Win32
OS: Win10

AB#38543222

@sz5000 sz5000 added the bug Something isn't working label Mar 14, 2022
@sz5000
Copy link
Author

sz5000 commented Mar 15, 2022

I have prepared a sample project where you can see the problem. Just open and start it.

The sample app opens the devtools window and there you can follow the outputs in the console. Normally there should be alternating messages from test.html and iframe.html but if the message from iframe.html is not logged, that the iframe did not receive the message, the message was completely lost in that case which is a bug.

HRESULT of PostWebMessageAsJson is always S_OK, even if the message is lost.

Win32_GettingStarted_Postmessage_Iframe_Example.zip

The following image shows the console output in devtools, and here the messages from test.html and iframe.html should alternate what they sometimes do, but most of the time only test.html receives the message.

iframe_postmessage_bug

I hope this helps you to find the bug because I can't because I don't have access to the source code.

@champnic champnic added the tracked We are tracking this work internally. label Mar 15, 2022
@champnic
Copy link
Member

Thanks for the bug report, sample, and detailed description @sz5000! I'm sorry you're running into this. I've added this bug to our backlog to take a look.

@vbryh-msft
Copy link
Contributor

@sz5000 - the fix is available in Canary 101.0.1204.0 and up. Could you please see if it works as expected for you. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

3 participants