Disabling Features When Offline
You'll learn how to intelligently disable or hide features that depend on network connectivity while preserving offline-capable functionality.
The Core Idea
Not every feature in your Progressive Web App can work offline. Actions like real-time collaboration, payment processing, or submitting forms to a server require connectivity. Rather than letting users encounter cryptic errors, you should proactively disable or hide these features when offline, while keeping core functionality accessible.
Think of it like a restaurant during a power outage: the kitchen can't cook hot meals, but they can still serve cold beverages and pre-made items. You don't pretend the full menu is available—you clearly communicate what's possible right now.
Implementation Strategy
Using the navigator.onLine property and online/offline event listeners (concepts you've already covered), you can conditionally disable UI elements:
function updateFeatureAvailability() {
const requiresNetwork = document.querySelectorAll('[data-requires-network]');
requiresNetwork.forEach(element => {
if (navigator.onLine) {
element.disabled = false;
element.classList.remove('offline-disabled');
} else {
element.disabled = true;
element.classList.add('offline-disabled');
element.title = 'This feature requires an internet connection';
}
});
}
window.addEventListener('online', updateFeatureAvailability);
window.addEventListener('offline', updateFeatureAvailability);
updateFeatureAvailability(); // Initial check
This approach marks dependent features with data-requires-network attributes, then systematically disables them when offline. Buttons become unclickable, forms get disabled, and visual feedback (via CSS classes) signals why.
User Experience Matters
Always provide clear feedback: a tooltip explaining "Available when online" is far better than a silently broken button. This builds trust and sets proper expectations.
Key Takeaway: Gracefully disable network-dependent features when offline using connectivity detection, preventing user frustration while highlighting what remains available—transparency creates better offline experiences.