Skip to content

Commit

Permalink
Fix: detect if iframes are already loaded. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Mar 7, 2024
1 parent 06d3efb commit a8a6c04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/breezy-ads-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rpc-anywhere": minor
---

Fix: detect if iframes are already loaded.
6 changes: 5 additions & 1 deletion src/transports/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const IFRAME_MSG_KEY = "[iframe-transport]";
const IFRAME_READY_MSG = "[iframe-transport-ready]";

async function waitForLoad(element: HTMLElement) {
return new Promise((resolve) => element.addEventListener("load", resolve));
const readyState = (element as HTMLIFrameElement).contentDocument?.readyState;
const location = (element as HTMLIFrameElement).contentWindow?.location.href;
return location !== "about:blank" && readyState === "complete"
? Promise.resolve()
: new Promise((resolve) => element.addEventListener("load", resolve));
}

async function portReadyPromise(port: MessagePort) {
Expand Down

0 comments on commit a8a6c04

Please sign in to comment.