npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

offline-detector

v1.1.1

Published

A lightweight TypeScript library for detecting online/offline status in browsers with modern bundler support. Minimal dependencies, tree-shakable, and works with any bundler.

Downloads

685

Readme

Offline Detector

npm version License: MIT Bundle Size Tests PRs Welcome

A lightweight TypeScript library for detecting online/offline status in browsers with modern bundler support. Features intelligent network verification, debounced state changes, and flexible configuration options.

Features

  • 🌐 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

Installation

npm install offline-detector

Quick Start

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();

API Reference

createOfflineDetector(options?)

Creates a new offline detector instance.

Parameters

  • options (optional): OfflineDetectorOptions - Configuration object

Returns

OfflineDetector - An object with methods to control the detector

OfflineDetectorOptions

interface OfflineDetectorOptions {
  /** Callback function called when the device comes online */
  onOnline?: () => void;
  /** Callback function called when the device goes offline */
  onOffline?: () => void;
  /** Debounce delay for state changes in milliseconds. Defaults to 1000ms */
  stateChangeDebounceDelay?: number;
  /** Network verification and polling configuration */
  networkVerification?: {
    /** Whether to perform actual network requests for verification. Defaults to true */
    enabled?: boolean;
    /** URL to test connectivity against. Defaults to a reliable endpoint */
    url?: string;
    /** Request timeout for connectivity tests in milliseconds. Defaults to 5000ms */
    requestTimeout?: number;
    /** Interval between connectivity checks in milliseconds. Defaults to 60000ms */
    interval?: number;
    /** Maximum consecutive failures before considering offline. Defaults to 3 */
    maxFailures?: number;
  };
  /** Native events configuration */
  nativeEvents?: {
    /** Whether to enable browser's native online/offline events as primary detection. Defaults to true */
    enabled?: boolean;
  };
}

OfflineDetector

interface OfflineDetector {
  /** Start monitoring network status */
  start(): void;
  /** Stop monitoring network status */
  stop(): void;
  /** Get current online status - returns true if online, false if offline */
  isOnline(): boolean;
  /** Destroy the detector and clean up resources */
  destroy(): void;
}

Usage Examples

Basic Usage

import { createOfflineDetector } from 'offline-detector';

const detector = createOfflineDetector({
  onOnline: () => {
    console.log('Connection restored!');
    // Show success notification
  },
  onOffline: () => {
    console.log('Connection lost!');
    // Show offline indicator
  },
});

detector.start();

Advanced Configuration

import { createOfflineDetector } from 'offline-detector';

const detector = createOfflineDetector({
  onOnline: () => {
    // Sync data when back online
    syncPendingData();
  },
  onOffline: () => {
    // Show offline banner
    showOfflineBanner();
  },
  stateChangeDebounceDelay: 2000, // Wait 2 seconds before triggering callbacks
  networkVerification: {
    enabled: true,
    url: 'https://api.example.com/health', // Your own endpoint
    requestTimeout: 10000, // 10 second timeout
    interval: 30000, // Check every 30 seconds
    maxFailures: 2, // Go offline after 2 consecutive failures
  },
  nativeEvents: {
    enabled: true, // Use browser's native events as primary detection
  },
});

detector.start();

React Hook Example

import { useEffect, useState } from 'react';
import { createOfflineDetector } from 'offline-detector';

function useOfflineDetector() {
  const [isOnline, setIsOnline] = useState(true);

  useEffect(() => {
    const detector = createOfflineDetector({
      onOnline: () => setIsOnline(true),
      onOffline: () => setIsOnline(false),
      networkVerification: {
        enabled: true,
        interval: 5000, // Check every 5 seconds
        maxFailures: 2
      }
    });

    detector.start();
    setIsOnline(detector.isOnline());

    return () => detector.destroy();
  }, []);

  return isOnline;
}

// Usage in component
function App() {
  const isOnline = useOfflineDetector();

  return (
    <div>
      <h1>My App</h1>
      {!isOnline && (
        <div className="offline-banner">
          You're currently offline
        </div>
      )}
    </div>
  );
}

Vanilla JavaScript Example

import { createOfflineDetector } from 'offline-detector';

// Create detector with custom configuration
const detector = createOfflineDetector({
  onOnline: () => {
    document.body.classList.remove('offline');
    document.getElementById('status').textContent = 'Online';
  },
  onOffline: () => {
    document.body.classList.add('offline');
    document.getElementById('status').textContent = 'Offline';
  },
  stateChangeDebounceDelay: 1500,
  networkVerification: {
    enabled: true,
    url: '/api/health',
    requestTimeout: 3000,
    interval: 10000,
    maxFailures: 3,
  },
});

// Start monitoring
detector.start();

// Check status programmatically
function checkStatus() {
  const status = detector.isOnline() ? 'Online' : 'Offline';
  console.log(`Current status: ${status}`);
}

// Clean up when done
window.addEventListener('beforeunload', () => {
  detector.destroy();
});

Module Bundlers

ES Modules

import { createOfflineDetector } from 'offline-detector';

CommonJS

const { createOfflineDetector } = require('offline-detector');

Configuration Options

Network Verification

The library can perform actual network requests to verify connectivity:

  • enabled: Enable/disable network verification (default: true)
  • url: Endpoint to test against (default: 'https://www.google.com/favicon.ico')
  • requestTimeout: Request timeout in milliseconds (default: 5000)
  • interval: Check interval in milliseconds (default: 60000)
  • maxFailures: Consecutive failures before going offline (default: 3)

Native Events

Uses browser's built-in online/offline events:

  • enabled: Enable/disable native events (default: true)

Debouncing

Prevents rapid state changes:

  • stateChangeDebounceDelay: Delay in milliseconds before triggering callbacks (default: 1000)

License

MIT

Contributing

See CONTRIBUTING.md for details.

Development

# 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