@artydev I would go one step further and put the status value as a class on the root html node. That way any other online/offline dependent elements can be captured. After all, the status is an aspect of the document as a whole!
A Tech Lead at monday.com with over a decade of experience, specializing in developer tools, client architecture, and optimizing workflows to enhance developer velocity and confidence.
Hi Chris, thank you for this article! It was very interesting. Keep in mind that relying on the browser alone may not be enough. In some scenarios the browser may falsly "think" it is connected, for example if connected to a network that has no connection to the internet. If the status is "offline" then there's no connection, but "online" does not necessarily mean a connection exists.
Yeah correct, it's very rare you'll need this much detail. In general for specific type of applications that would need this I think this will do, but if it highly depends on the status there are alternatives indeed.
Hi Chirs, you can get true even when device can't access internet. Such as using software that use virtual ethernet adapters that are always "connected." Do you know something for that as well?
Dear Chris Bongers,may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.
I am a professional front-end developer who has special knacks in creating pixel perfect interactive websites and web applications that solves business problems. I have also written lots of articles
This API is too unstable and usually you don't want to use it in production, because online in terms of user's browser may not be the same as online in terms of the user themselves.
There are better ways to detect offline:
Long-pooling request to the server.
Periodical check for a dummy image/resource without caching.
Showing such a status only when the user's request fails due to unstable network.
Relying on navigator.onLine only you may scare your user. So, please do not.
I am a professional DevOps Engineer with a demonstrated history of working in the internet industry. I am an avid Linux lover and supporter of the open-source movement philosophy.
Great Chris as usual :-)
A detail, instead of
Why not
And in your CSS
Regards
We could do that indeed, really depends on the setup, but yeah this is quite neat 🥳
@artydev I would go one step further and put the status value as a class on the root html node. That way any other online/offline dependent elements can be captured. After all, the status is an aspect of the document as a whole!
Elements that are only available when online could be given the class
online-only
.Thank you Bill, nice idea.
I have never thought to set a class on html tag...
Hi Chris, thank you for this article! It was very interesting.
Keep in mind that relying on the browser alone may not be enough. In some scenarios the browser may falsly "think" it is connected, for example if connected to a network that has no connection to the internet. If the status is "offline" then there's no connection, but "online" does not necessarily mean a connection exists.
Yeah correct, it's very rare you'll need this much detail.
In general for specific type of applications that would need this I think this will do, but if it highly depends on the status there are alternatives indeed.
In case anyone uses react, here's a solution:
`import React, { useState, useEffect } from 'react';
import './OnlineStatus.css';
export default function OnlineStatus() {
const [status, setStatus] = useState('offline');
}`
Hi Chirs, you can get true even when device can't access internet. Such as using software that use virtual ethernet adapters that are always "connected." Do you know something for that as well?
That's very tricky, think only server side would be able to handle those events?
Like try to reach something (ping) and see if it actually responds?
Dear Chris Bongers,may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.
Sure thing, go for it 🙏
Very useful. Thank you.
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍
Amazing! 🥳
This is nice..
Amazing
Thanks for sharing
Thanks! glad you enjoyed it ❤️
This API is too unstable and usually you don't want to use it in production, because
online
in terms of user's browser may not be the same asonline
in terms of the user themselves.There are better ways to detect offline:
Relying on
navigator.onLine
only you may scare your user. So, please do not.Very cool!
Great one. Kindly check it out too dev.to/lovepreetsingh/oops-write-s...
@lovepreetsingh
You could be interested by Learning JavaScript Design Patterns
from wellknown Addy Osmani
Regards
Sure, I am going to check this one
Hi Chris, thank you!
BTW, what design tool are you using for your featured image?