jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.13k stars 270 forks source link

Fixed an problem about invalid iframe contentWindow (iframe not be prepared as our expectation) #204

Open BorisJineman opened 6 years ago

BorisJineman commented 6 years ago

Problem:

When I use this library in my complex network environment, I found a tiny problem, bellow is the reproduce steps,

  1. Set network connection to offline, (unplug the network cable and disconnected the wireless network)

  2. Try to use xdomain to retrieve something from internet, (it should failed with timeout event, due to its created iframe cannot load the proxy.html page)

  3. Problem phenomenon, There will be an error thrown with message "Invalid calling object" when we call frame.postMessage, (It should caused by the object frame is not prepared well or it is retired) Thus, the next logic about timeout checking logic will be broken. So the whole timeout trigger logic will not work.

There is a rare situation, but it indeed exists when the network is offline.

I found I cannot reproduce above issue in Chrome and Safari, even in IE browser, But I can reproduce it in a application hosted Web Browser control. (I already set the Feature Control Key of the module to 11001, so it should simulate the IE11 Edge)

Solution:

I think this should be a tiny issue, and we could just wait the iframe be loaded in time. We also have a timeout timer to check whether it is loaded in time, so I think we can just ignore this error,

In this PR, I just added a try to it, thus, the countdown timer of timeout can work, so its trigger logic will be corrected.

@eligrey @mscdex @jpsirois @soncodi @NathanWalker , Could you have a look on this PR and review it?, thanks a lot,