3

Our team has started using Offline.js, a library that can detect if a user is offline. We have currently set it up to look for an image on our server using the following code:

Offline.options = {checks: {xhr: {url: 'resources/bower/offline/img/tiny-image.gif'}}};

However, we have found that if our server goes down, the end user gets an error saying that their internet is disconnected, which is not true. Their internet is fine, it is a problem on our end.

I do not believe it is possible to check with a website that has more reliable uptime; it must be something on our server. From the Offline.js documentation:

Make sure that the URL you check has the same origin as your page (the connection method, domain and port all must be the same), or you will run into CORS issues.

So how can I make Offline.js distinguish between genuine offline behavior and the server being down?

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • 3
    What if you added a second check that hit something hit something static on a CDN so that it won't likely go down? – Mark Meyer Aug 10 '15 at 17:50
  • @NuclearGhost I wasn't aware you could have two checks. How would I do that? Do I make the value of `xhr` an array? Also, I'm not sure I fully understand what you mean about hitting something static on a CDN. – Thunderforge Aug 10 '15 at 17:56
  • So, your server goes down intermittently and your solution is to _detect_ the problem, no resolve it? Seems like a XY problem. -- And CDN (Content Delivery Network). Who's to say you have to detect your server being down? If you have a history of outages, use something more reliant like `code.jquery.com` or others. – Brad Christie Aug 10 '15 at 17:58
  • @BradChristie The purpose of Offline.js is to notify the user that there is a client error that is preventing them from connecting to the internet. Maybe their router quit working or they unplugged their ethernet cord. So there isn't really any plan to resolve the error. Also, Offline.js requires whatever you are checking to be the same origin as your page. So I don't think it will be possible to connect to `code.jquery.com` – Thunderforge Aug 10 '15 at 18:24
  • If you are using AWS (or a similar service), setup a subdomain that points to AWS instance, and not your server. [How to put a subdomain on a different server](https://css-tricks.com/put-a-subdomain-on-a-different-server/). Cross check with that subdomain if the server url doesn't respond. This is not 100% fool proof, but should work most of the time. – aarjithn Aug 17 '15 at 16:13
  • 1
    How about you change the message? The application is probably supposed to behave the same regardless of the exact reason that it cannot phone home to the server. It is only the message that is incorrect. – flup Aug 18 '15 at 06:20
  • back link https://github.com/HubSpot/offline/issues/158 – Anatoly Aug 19 '15 at 15:38

1 Answers1

3

I saw this in the offline.js docs at https://github.com/hubspot/offline:

If you do want to run tests on a different domain, try the image method. It loads an image, which are allowed to cross domains.

Offline.options = {checks: {image: {url: 'my-image.gif'}, active: 'image'}}

So you could use something like the google logo:

Offline.options = {checks: {image: {url: 'https://www.google.com/images/srpr/logo11w.png'}, active: 'image'}}
Clint
  • 156
  • 3