vue-plugin-load-script
vue-plugin-load-script copied to clipboard
Can not deal with timeout
When my network is not allowed to access the scrips, I can't deal with the timeout error.
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 It doesn't work. The console still print " net::ERR_CONNECTION_TIMED_OUT "
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