A lightweight TypeScript library for detecting online/offline status in browsers with modern bundler support.
- π Browser-focused: Designed specifically for browser environments
- π¦ Bundler agnostic: Works with any modern bundler (Webpack, Vite, Rollup, etc.)
- π§ TypeScript: Full TypeScript support with type definitions
- π Lightweight: Minimal bundle size with tree-shaking support
- π± Cross-platform: Works across all modern browsers
- π Smart Detection: Combines native events with network verification
- β‘ Debounced: Prevents rapid state changes with configurable debouncing
- π― Configurable: Extensive options for customization
npm install offline-detector
import { createOfflineDetector } from 'offline-detector';
const detector = createOfflineDetector({
onOnline: () => console.log('Back online!'),
onOffline: () => console.log('Gone offline!'),
});
// Start monitoring
detector.start();
// Check current status
console.log(detector.isOnline()); // true or false
// Stop monitoring
detector.stop();
This is a monorepo containing multiple packages:
offline-detector
- Core detection library with comprehensive API documentationoffline-detector/react
- React hooks and components (coming soon)offline-detector/vue
- Vue composables and components (coming soon)
The core API is simple and intuitive:
const detector = createOfflineDetector({
onOnline: () => console.log('Back online!'),
onOffline: () => console.log('Gone offline!'),
});
detector.start(); // Start monitoring
detector.isOnline(); // Check current status
detector.stop(); // Stop monitoring
detector.destroy(); // Clean up resources
π View complete API documentation β
const detector = createOfflineDetector({
onOnline: () => syncPendingData(),
onOffline: () => showOfflineBanner(),
stateChangeDebounceDelay: 2000,
networkVerification: {
enabled: true,
url: 'https://api.example.com/health',
interval: 30000,
maxFailures: 2,
},
});
function useOfflineDetector() {
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
const detector = createOfflineDetector({
onOnline: () => setIsOnline(true),
onOffline: () => setIsOnline(false),
});
detector.start();
return () => detector.destroy();
}, []);
return isOnline;
}
π View more examples and detailed configuration β
Security is important to us. If you discover a security vulnerability, please follow our Security Policy for responsible disclosure.
See CONTRIBUTING.md for details.
# Install dependencies
npm install
# Start development mode
npm run dev
# Build the library
npm run build
# Run linting
npm run lint
# Format code
npm run format
- Repository setup and boilerplate
- TypeScript configuration
- Modern bundler support (Rollup)
- ESLint and Prettier setup
- Basic online/offline detection implementation
- API documentation
- Example website/demo configuration
- Testing framework setup (Jest/Vitest)
- Unit tests and CI/CD
- Network quality detection
- Custom polling strategies
- Connection verification with configurable test URLs
- Debounced state changes
- Failure threshold handling
- Connection type detection
- Convert to monorepo structure (Lerna)
- [] Split into multiple packages:
offline-detector
- Core detection logicoffline-detector/react
- React hooks and componentsoffline-detector/vue
- Vue composables and componentsoffline-detector/svelte
- Svelte stores and componentsoffline-detector/angular
- Angular services and directives
- Shared tooling and configuration
- Independent versioning and publishing (Changesets)
- Cross-package testing and validation
- Comprehensive testing suite
- Performance benchmarks
- Browser compatibility testing