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.

circle-info

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

Type: 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

Type: 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

HTML 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

HTML 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

Dispatched whenever the connection status changes (online ↔ offline).

Payload:

  • status (string): Either "ONLINE" or "OFFLINE"

  • message (string): The current message (either onlineMessage or offlineMessage)

Example:

Best Practices

  1. Provide clear feedback: Always inform users about their connection status

  2. Graceful degradation: Disable online-only features when offline

  3. Queue operations: Store user actions locally and sync when connection is restored

  4. Save drafts: Auto-save content locally to prevent data loss

  5. Visual indicators: Use clear, consistent visual cues for connection status

  6. Don't block: Allow users to continue working offline when possible

  7. Optimize for mobile: Connection status is particularly important on mobile devices

  8. Test thoroughly: Test your application's behavior in various connectivity scenarios

  9. Handle edge cases: Consider slow connections, intermittent connectivity

  10. 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 message target is properly set

  • Check that parameters onlineMessage and offlineMessage are configured

  • Inspect the element to ensure it's receiving content

Styling not applying

Issue: CSS classes based on data-connection-status not working

Solutions:

  • Ensure the attribute target is set on the correct element

  • Check CSS selector specificity

  • Verify Tailwind variants are configured if using Tailwind CSS

  • Use browser inspector to confirm the attribute is being set

Browser Compatibility

Browser
Support

Chrome

✓ Full support

Firefox

✓ Full support

Safari

✓ Full support

Edge

✓ Full support

Opera

✓ Full support

Mobile browsers

✓ Full support

circle-check

Last updated

Was this helpful?