vue-plugin-load-script icon indicating copy to clipboard operation
vue-plugin-load-script copied to clipboard

Can not deal with timeout

Open gjwfiza opened this issue 6 years ago • 3 comments

When my network is not allowed to access the scrips, I can't deal with the timeout error.

gjwfiza avatar Jan 09 '19 08:01 gjwfiza

loadScript(...) returns a promise, and is rejected when the injected <script> emits an error or abort event, so a .catch(...) on the returned promise should allow you to handle the error.

Does the following usage not catch timeouts?

this.$loadScript('//unreachable.host.com/script.js')
    .then(() => alert('script loaded!'))
    .catch((err) => alert('script error!\n' + err));

tserkov avatar Jan 09 '19 16:01 tserkov

@tserkov It doesn't work. The console still print " net::ERR_CONNECTION_TIMED_OUT "

gjwfiza avatar Jan 10 '19 00:01 gjwfiza

After some research, this looks like this is a DOM limitation. The onerror event does not fire for network events, and there are no other error-type events that fire. The general solution seems to implement a timeout (using setTimeout) and checking if the object you're expecting from the loaded script exists.

With that said, the spec also says the element is set to null on load failure. So while that would require polling, it's certainly doable by this plugin. I'll set some time aside tonight to add a timeout poll that would check if the variable holding the element is suddenly null and reject the promise.

References:

  • https://stackoverflow.com/questions/538745/how-to-tell-if-a-script-tag-failed-to-load
  • http://www.w3.org/TR/html5/scripting-1.html#script

tserkov avatar Jan 10 '19 15:01 tserkov