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

use-visibility-polling

v1.0.0

Published

React polling hook that pauses when the tab is hidden or offline, refetches instantly when the user comes back, never overlaps requests, backs off on errors and supports abort. Saves server load and battery.

Readme

use-visibility-polling

npm size

Polling that stops when nobody is looking. setInterval(fetch, 5000) keeps hammering your API from every forgotten background tab. This hook pauses while the tab is hidden or offline, refetches immediately when the user comes back, never overlaps requests, aborts stale ones and backs off on errors.

import { useVisibilityPolling } from 'use-visibility-polling';

function ServerStats() {
  const { data, error, isPaused, lastUpdated, refresh } = useVisibilityPolling(
    (signal) => fetch('/api/stats', { signal }).then((r) => r.json()),
    { interval: 10_000 },
  );

  return (
    <section>
      <Stats data={data} />
      <small>
        {isPaused ? 'Paused' : `Updated ${lastUpdated?.toLocaleTimeString()}`}
        <button onClick={refresh}>Refresh</button>
      </small>
      {error && <p>Failed to load, retrying…</p>}
    </section>
  );
}

Install

npm install use-visibility-polling

Behaviour

| Situation | What happens | | --- | --- | | Tab hidden / minimized | Timer cleared, in-flight request aborted | | Tab visible again | Fetch immediately if data is older than interval, then resume | | Offline → online | Same as above | | Slow request | Next call starts interval ms after it finishes — never overlapping | | Errors | Backoff: interval·2ⁿ up to maxBackoff (default 60 s); resets on success | | Unmount | Timer cleared, request aborted, no state updates |

Options

| Option | Default | Description | | --- | --- | --- | | interval | 5000 | ms between calls | | enabled | true | Start/stop | | immediate | true | Fetch on mount | | pauseWhenHidden | true | Use the Page Visibility API | | pauseWhenOffline | true | Use navigator.onLine | | refetchOnResume | true | Fetch right away on return if stale | | maxBackoff | 60000 | Error backoff cap, false to disable | | onSuccess(data), onError(error) | – | Callbacks |

Returns { data, error, isLoading, isFetching, isPaused, lastUpdated, refresh() }. Alias: usePolling. Also exported: usePageVisibility().

With React Query?

React Query already has refetchInterval + refetchIntervalInBackground: false. Use this hook when you don't use a data-fetching library, or for non-HTTP polling (device status, local APIs, SDK calls).

License

MIT