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 🙏

© 2024 – Pkg Stats / Ryan Hefner

offline-sync-handler

v0.1.11

Published

**Offline Sync Provider - README**

Downloads

2

Readme

Offline Sync Provider - README

Offline Sync Provider

Description

Offline Sync Provider is a JavaScript module designed to handle API requests in web applications with offline capabilities. It offers a robust solution to synchronize data with the server even when the device is offline and automatically retries failed requests upon reconnection. This module utilizes axios for making API requests and localforage for offline storage, ensuring data integrity and smooth synchronization.

Installation

$ npm install --save offline-sync-handler
$ yarn add offline-sync-handler

Demo

You can find the working demo here

Usage

Offline Sync Provider

Wrap your application with the OfflineSyncProvider component to enable offline sync and manage data synchronization.

import { OfflineSyncProvider } from 'offline-sync-handler';
const App = () => {
  // Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider>
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Sending API Requests

You can use the sendRequest function to send API requests. It handles retries in case of failure due to an unstable internet connection. Refer to the axios-create documentation for available config options.

import { useOfflineSyncContext } from from 'offline-sync-handler';
  const { sendRequest } = useOfflineSyncContext();
  const config = {
    method: 'POST',
    url: 'https://api.example.com/data',
    data: { name: 'John Doe', email: '[email protected]' },
  };

try {
  const response = await sendRequest(config);
  console.log('API Response:', response);
} catch (error) {
  console.error('API Request failed:', error.message);
}

Passing Custom Component to display during Offline

You can pass the custom component to show during offline using the render prop of the OfflineSyncProvider.


import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
  // Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider
    render={({ isOnline }) => {
      return isOnline ? null : <div>I am offline</div>;
    }}
  >
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Track online status change to perform certain operation

You can track online status change using the onStatusChange prop of the OfflineSyncProvider.

import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
  // Your application components and logic
};


const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider
    onStatusChange={(status)=>{
      console.log({status})
    }}
  >
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Usage with NextJS

You can dynamically import this package to use it with NextJs.

export default function App({ Component, pageProps }: AppProps) {
  const [packageModule, setPackageModule] = useState<{
    OfflineSyncProvider: any | undefined;
  }>();

  useEffect(() => {
    const fetchPackage = async () => {
      try {
        const packageModule = await import("offline-sync-handler-test");
        setPackageModule(packageModule);
      } catch (error) {
        console.error("Error loading the npm package:", error);
      }
    };
    fetchPackage();
  }, []);

  if (packageModule) {
    return (
      <packageModule.OfflineSyncProvider
        render={renderOffline}
      >
        <>
          <Component {...pageProps} />
        </>
      </packageModule.OfflineSyncProvider>
    );
  }

  return (
    <>
      <Component {...pageProps} />
    </>
  );
}

Passing Sync Success Callback

You can handle the Sync Success respnse by using the onSyncSuccess prop of the OfflineSyncProvider.


export default function App({ Component, pageProps }: AppProps) {

  const onSyncSuccess = (response: any) => {
    // response consist of config and data where config is the object passed to sendRequest and data is the api response
    console.log({response});
  };

    return (
      <OfflineSyncProvider
        onSyncSuccess={onSyncSuccess}
      >
        <>
          <Component {...pageProps} />
        </>
      </OfflineSyncProvider>
    );
  }

Roadmaps

  • [x] Passing callbacks functions to handle the success callback.
  • [x] Proper NextJs Support.

License

This project is licensed under the MIT License.

Issues and Contributions

If you encounter any issues or have suggestions for improvement, please submit an issue. Contributions are welcome! Please fork the repository and create a pull request.


Thank you for using the Offline Sync Provider module! We hope it simplifies handling API requests and enhances the offline experience for your web application. If you have any questions or need further assistance, feel free to reach out to us. Happy coding!