yari icon indicating copy to clipboard operation
yari copied to clipboard

fix: interactive iframe post message error

Open OnkarRuikar opened this issue 3 years ago • 1 comments

Summary

Pages with interactive examples throw following error because yari tries to send theme change message before the iframe content is loaded:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://interactive-examples.mdn.mozilla.net') does not match the recipient window's origin ('https://developer.mozilla.org').

Problem

Steps to reproduce:

  1. Open page https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit.
  2. Set them to "Dark".
  3. Do force page reload using Ctrl + F5.
  4. In the logs you'll see the error message.

The problem is the iframe content takes time to load but yari starts sending theme change message multiple times. The iframe with empty content throws origin mismatch error. Also, yari sends the message multiple times due code error.

Solution

Call postMessage on the iframe only if the content is loaded. But with SSR involved it's bit trickier. Finding out the content finished loading, for other origin iframe, is not possible without using onload event. And with SSR it's not possible to use onload event inside the React. Because browser immediately starts loading the iframe came directly from server, and useEffect events registration inside React happens after components are rendered. More info here.

So, as suggested by Dan, we can listen for the iframe load event on document body and register the load finished.

Screenshots

Before

error

How did you test this change?

On a Chromium browser on Fedora.

OnkarRuikar avatar Jun 18 '22 02:06 OnkarRuikar

Signed the commit.

OnkarRuikar avatar Jun 27 '22 12:06 OnkarRuikar

The error is not there in the deployed app. \o/ interactiveFix

OnkarRuikar avatar Aug 24 '22 15:08 OnkarRuikar

Thank you @OnkarRuikar! 🎉

caugner avatar Sep 02 '22 13:09 caugner