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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-offline-sync-hook

v1.0.7

Published

A comprehensive React hook for offline data synchronization with automatic conflict resolution and queue management

Readme

React Offline Sync Hook 🗄️

The website will work even when the network is offline.

A comprehensive React hook for offline data synchronization with automatic conflict resolution, queue management, and background sync capabilities.

License: MIT TypeScript

✅ Features

  1. React HookuseOfflineSync (core)
  2. LocalStorage Adapter → Fast, simple storage
  3. IndexedDB Adapter → Large data storage
  4. Data Queue Manager → Auto retry with exponential backoff
  5. Online/Offline Status → Real-time network monitoring
  6. Auto Sync When Online → Seamless data synchronization
  7. Conflict Resolution (LWW) → Last-write-wins strategy
  8. Custom Conflict Handler → User-defined resolution logic
  9. Manual Sync Trigger → On-demand synchronization
  10. Background Sync → Service Worker integration
  11. Batching Requests → Optimized network usage
  12. Error Logging System → Comprehensive error tracking
  13. Retry with Exponential Backoff → Smart retry mechanism
  14. Status API → Real-time sync status
  15. React Context Provider<OfflineSyncProvider>
  16. Hook Return Values → Complete API surface
  17. TypeScript Support → Full type safety
  18. JavaScript Support → Compiled builds
  19. Universal Support → Next.js, CRA, Remix
  20. Easy API Integration → Ready-to-use

🚀 Live Demo & Documentation

  • Coming Soon...

🚀 Installation

npm install react-offline-sync-hook
# or
yarn add react-offline-sync-hook
# or
pnpm add react-offline-sync-hook

📖 Quick Start

Basic Usage

import React from "react";
import { OfflineSyncProvider, useOfflineSync } from "react-offline-sync-hook";

// 1. Configure the sync behavior
const config = {
  storageAdapter: "indexedDB",
  apiEndpoint: "https://your-api.com/api/todos",
  retryAttempts: 3,
  conflictResolution: "last-write-wins",
  enableBackgroundSync: true,
};

// 2. Create your component
const TodoApp = () => {
  const { data, addItem, updateItem, deleteItem, status } = useOfflineSync(
    "todos",
    config
  );

  return (
    <div>
      <div>Status: {status.isOnline ? "🟢 Online" : "🔴 Offline"}</div>
      <button onClick={() => addItem({ title: "New Todo", completed: false })}>
        Add Todo
      </button>
      {data.map((todo) => (
        <div key={todo.id}>
          {todo.title}
          <button onClick={() => deleteItem(todo.id)}>Delete</button>
        </div>
      ))}
    </div>
  );
};

// 3. Wrap with Provider
const App = () => (
  <OfflineSyncProvider config={config}>
    <TodoApp />
  </OfflineSyncProvider>
);

🔧 Configuration Options

interface SyncConfig {
  // Storage
  storageAdapter: "localStorage" | "indexedDB";

  // API
  apiEndpoint: string;
  headers?: Record<string, string>;

  // Retry Logic
  retryAttempts?: number; // Default: 3
  retryDelay?: number; // Default: 1000ms

  // Batching
  batchSize?: number; // Default: 10

  // Conflict Resolution
  conflictResolution?:
    | "client-wins"
    | "server-wins"
    | "last-write-wins"
    | "custom";
  customConflictHandler?: (local: any, remote: any) => any;

  // Background Sync
  enableBackgroundSync?: boolean; // Default: false
  syncInterval?: number; // Auto-sync interval in ms
}

📚 API Reference

useOfflineSync Hook

const {
  data, // T[] - Your synchronized data
  status, // SyncStatus - Current sync state
  error, // Error | null - Last error
  syncNow, // () => Promise<void> - Manual sync
  addItem, // (item: Omit<T, 'id'>) => Promise<void>
  updateItem, // (id: string, updates: Partial<T>) => Promise<void>
  deleteItem, // (id: string) => Promise<void>
  clearData, // () => Promise<void>
} = useOfflineSync<T>(key, config, initialData);

SyncStatus Object

interface SyncStatus {
  isOnline: boolean; // Network connectivity
  isSyncing: boolean; // Currently syncing
  lastSync: Date | null; // Last successful sync
  pendingItems: number; // Items waiting to sync
  error: string | null; // Last error message
}

🛠 Advanced Usage

Custom Conflict Resolution

const config = {
  conflictResolution: "custom",
  customConflictHandler: (localData, remoteData) => {
    // Your custom merge logic
    return {
      ...localData,
      ...remoteData,
      title: `${localData.title} (merged)`,
      updatedAt: new Date().toISOString(),
    };
  },
};

Background Sync with Service Worker

// 1. Enable in config
const config = {
  enableBackgroundSync: true,
  // ... other options
};

// 2. Create service worker file: public/offline-sync-sw.js
// (The package provides a generator for this)

Multiple Data Types

const todosSync = useOfflineSync<Todo>("todos", todosConfig);
const notesSync = useOfflineSync<Note>("notes", notesConfig);
const contactsSync = useOfflineSync<Contact>("contacts", contactsConfig);

🔄 Storage Adapters

LocalStorage (Fast, Limited)

  • ✅ Fast access
  • ✅ Simple setup
  • ❌ ~5-10MB limit
  • ❌ Synchronous operations
const config = { storageAdapter: "localStorage" };

IndexedDB (Large, Async)

  • ✅ Large storage capacity
  • ✅ Asynchronous operations
  • ✅ Complex queries
  • ❌ Slightly more complex
const config = { storageAdapter: "indexedDB" };

🚦 Network Detection

The hook automatically detects network changes:

const { status } = useOfflineSync("data", config);

// React to network changes
useEffect(() => {
  if (status.isOnline) {
    console.log("Back online! Auto-syncing...");
  } else {
    console.log("Gone offline. Queuing changes...");
  }
}, [status.isOnline]);

🔁 Retry Mechanism

Built-in exponential backoff:

const config = {
  retryAttempts: 5, // Try 5 times
  retryDelay: 1000, // Start with 1 second
  // Delays: 1s, 2s, 4s, 8s, 16s
};

📊 Error Handling & Logging

const { error, status } = useOfflineSync("data", config);

// Handle errors
if (error) {
  console.error("Sync error:", error.message);
}

// Check sync status
if (status.error) {
  console.warn("Last sync failed:", status.error);
}

🌐 Framework Compatibility

Next.js

// pages/_app.tsx or app/layout.tsx
import { OfflineSyncProvider } from "react-offline-sync-hook";

export default function App({ Component, pageProps }) {
  return (
    <OfflineSyncProvider config={syncConfig}>
      <Component {...pageProps} />
    </OfflineSyncProvider>
  );
}

Create React App

// src/index.tsx
import { OfflineSyncProvider } from "react-offline-sync-hook";

ReactDOM.render(
  <OfflineSyncProvider config={syncConfig}>
    <App />
  </OfflineSyncProvider>,
  document.getElementById("root")
);

Remix

// app/root.tsx
import { OfflineSyncProvider } from "react-offline-sync-hook";

export default function App() {
  return (
    <html>
      <head />
      <body>
        <OfflineSyncProvider config={syncConfig}>
          <Outlet />
        </OfflineSyncProvider>
      </body>
    </html>
  );
}

🧪 Testing

npm test        # Run tests
npm run test:watch  # Watch mode

📦 Build & Publish

# Development
npm run dev

# Build
npm run build

# Lint
npm run lint

# Type check
npm run type-check

# Publish
npm publish

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

MIT © Biren Gohel

🙏 Acknowledgments

  • Inspired by modern offline-first applications
  • Built for the React ecosystem
  • Designed for real-world use cases

Made with ❤️ for the Biren Gohel