Connection Status
The Connection Status component monitors and reacts to changes in the user's internet connection status. This component provides a simple yet powerful way to adapt your application's behavior when the user's device goes offline or comes back online, ensuring a seamless user experience regardless of network conditions.
This component is particularly useful for:
Displaying connection status notifications to users
Disabling features that require internet connectivity when offline
Queueing actions for later when connection is restored
Showing offline-specific UI and content
Preventing data loss during connectivity issues
Providing feedback about network-dependent operations
Implementing offline-first application strategies
Enhancing Progressive Web App capabilities
Browser Support
The Connection Status API is based on the navigator.onLine property and online/offline events, which are supported by all modern browsers on desktop and mobile platforms.
Support level: Universal - works on all major browsers including Chrome, Firefox, Safari, Edge, and their mobile counterparts.
The navigator.onLine property only indicates whether the browser has a network connection, not whether internet access is actually available. A device might be connected to a local network but have no internet access.
Usage
Basic Connection Status Display
Styled Connection Notification
Conditional Feature Display
Form Behavior Based on Connection
Action Queue for Offline Operations
Real-Time Sync Indicator
Multiple Status Indicators
Parameters
onlineMessage
onlineMessageType: string Default: "You are online"
The message displayed when the user is online. This message is injected into all elements marked with the message target.
offlineMessage
offlineMessageType: string Default: "You are offline"
The message displayed when the user is offline. This message is injected into all elements marked with the message target.
Actions
None - This component automatically monitors connection status without requiring explicit actions.
Targets
message
messageHTML elements that will display the connection status message. The content of these elements will be replaced with either onlineMessage or offlineMessage depending on the current connection status.
Multiple targets allowed: Yes - you can have multiple message targets throughout your application.
attribute
attributeHTML elements that will receive a data-connection-status attribute set to either "ONLINE" or "OFFLINE". This is particularly useful for conditional styling with CSS.
Multiple targets allowed: Yes - you can mark multiple elements with this target.
The data-connection-status attribute can be used with CSS attribute selectors:
Or with Tailwind CSS variants:
Events
pwa--connection-status:status-changed
pwa--connection-status:status-changedDispatched whenever the connection status changes (online ↔ offline).
Payload:
status(string): Either"ONLINE"or"OFFLINE"message(string): The current message (eitheronlineMessageorofflineMessage)
Example:
Best Practices
Provide clear feedback: Always inform users about their connection status
Graceful degradation: Disable online-only features when offline
Queue operations: Store user actions locally and sync when connection is restored
Save drafts: Auto-save content locally to prevent data loss
Visual indicators: Use clear, consistent visual cues for connection status
Don't block: Allow users to continue working offline when possible
Optimize for mobile: Connection status is particularly important on mobile devices
Test thoroughly: Test your application's behavior in various connectivity scenarios
Handle edge cases: Consider slow connections, intermittent connectivity
Inform, don't alarm: Frame offline messages positively when possible
Connection-Aware CSS Patterns
Using Tailwind CSS
If you're using Tailwind CSS, you can create custom variants for connection status:
Then use these variants in your HTML:
Using Standard CSS
Complete Example: Offline-First Todo App
Troubleshooting
Status not updating
Issue: Connection status doesn't change when network connectivity changes
Solutions:
Ensure the controller is properly initialized
Check browser console for JavaScript errors
Verify that event listeners are attached correctly
Test with browser DevTools network throttling
False positives
Issue: Shows "online" even when internet is not accessible
Cause: navigator.onLine only checks browser's network connection, not actual internet access
Solution: Implement additional checks:
Message not displaying
Issue: Connection status message doesn't appear
Solutions:
Verify the
messagetarget is properly setCheck that parameters
onlineMessageandofflineMessageare configuredInspect the element to ensure it's receiving content
Styling not applying
Issue: CSS classes based on data-connection-status not working
Solutions:
Ensure the
attributetarget is set on the correct elementCheck CSS selector specificity
Verify Tailwind variants are configured if using Tailwind CSS
Use browser inspector to confirm the attribute is being set
Browser Compatibility
Chrome
✓ Full support
Firefox
✓ Full support
Safari
✓ Full support
Edge
✓ Full support
Opera
✓ Full support
Mobile browsers
✓ Full support
The Connection Status component works reliably across all modern browsers and is based on well-established web platform APIs.
Last updated
Was this helpful?