Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect failed page load? #3525

Closed
johnthedebs opened this issue May 27, 2015 · 4 comments
Closed

How to detect failed page load? #3525

johnthedebs opened this issue May 27, 2015 · 4 comments

Comments

@johnthedebs
Copy link

I'm using NW.js to load a remote site by setting window.location.href, and periodically reloading the app (to refresh the data after coming back from sleep, for example).

Sometimes, the reload fails and if I inspect the value of window.location.href it's set to data:text/html,chromewebdata rather than the URL I set it to, which I'm taking to mean it failed to load the page for some reason.

I put a guard in place so that the reload wouldn't happen unless there's an active network connection (by resolving the DNS records of the URL being loaded before reloading), but in many cases the online check succeeds and the reload still fails.

Any ideas why this is happening? Is there a better way to guard against failed loads, or a way to check that a page has failed to load (or be notified about it)?

@baconbrad
Copy link

Have NW.js load a local HTML file with a script to check. Then use one of these two methods.

  • Load it in a full width/height iframe and use the onload event to check the status. I would start a 5 second timeout every time you do a reload and then onload should cancel it. If it doesn't cancel make it fire a function to handle the load failure.
  • Or set a timeout function that happens after 3-5 seconds or however you want that checks the window location. If it equals data:text/html,chromewebdata fire a function to handle the load failure.

You might have to set these timers higher based on the web servers response time.

@baconbrad
Copy link

And if you want to see if the server itself is down you can do a quick check with require("http");. For example, run the url through http.get to see if the response failed. This is built in so no need to fetch it from NPM.

@johnthedebs
Copy link
Author

@baconface Hey, thanks so much for the advice! I was struggling with this for a while and was finally able to make it work in a robust way with your advice about hooking into the iframe's onload event. I was actually looking over the nwFiddle code as I was working on this and picked up some stuff from that as well. Thanks again!

@baconbrad
Copy link

@johnthedebs Awesome, glad it was of help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants