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

@countcachula/react

v0.0.1

Published

React hooks for Count Cachula - stale-while-revalidate caching with Suspense support

Readme

@countcachula/react

React hooks for Count Cachula - stale-while-revalidate caching with Suspense support.

Installation

npm install @countcachula/react @countcachula/core

Usage

Basic Example with Suspense

import { Suspense } from 'react';
import { useFetch } from '@countcachula/react';

function IssueList() {
  // This suspends until data is available
  const issues = useFetch<Issue[]>('/api/issues');

  // Component only renders when data exists - no loading state needed!
  return (
    <ul>
      {issues.map(issue => (
        <li key={issue.id}>{issue.title}</li>
      ))}
    </ul>
  );
}

function App() {
  return (
    <ErrorBoundary fallback={<ErrorMessage />}>
      <Suspense fallback={<LoadingSpinner />}>
        <IssueList />
      </Suspense>
    </ErrorBoundary>
  );
}

With Authentication Headers

function ProtectedData() {
  const data = useFetch<UserData>('/api/user', {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });

  return <div>{data.name}</div>;
}

Custom Transform Function

function TextContent() {
  const content = useFetch<string>('/api/readme.txt', {
    transform: async (response) => response.text(),
  });

  return <pre>{content}</pre>;
}

SSE Connection for Real-time Updates

import { useConnection } from '@countcachula/react';

function App() {
  // Establish SSE connection for cache invalidation
  useConnection('/api/cache-events', {
    preload: {
      onHint: true,
      maxConcurrent: 3,
    },
  });

  return <YourApp />;
}

API Reference

useFetch<T>(url: string, options?: FetchOptions): T

Fetches data with automatic caching and Suspense support.

Parameters:

  • url: The URL to fetch
  • options: Optional fetch configuration
    • headers: Request headers
    • transform: Custom transform function (defaults to JSON parsing)
    • All other standard RequestInit options except method

Returns:

The fetched and typed data. Never returns undefined or loading states.

Behavior:

  1. First render (no cache): Suspends until data arrives
  2. First render (with cache): Returns cached data immediately, fetches fresh data in background
  3. Fresh data arrives: Component re-renders with new data, no loading state
  4. Errors: Throws to nearest Error Boundary

useConnection(endpoint: string, options?: ConnectionOptions): void

Manages SSE connection for real-time cache invalidation.

Parameters:

  • endpoint: SSE endpoint URL
  • options: Connection configuration
    • preload: Preloading configuration
      • onInvalidate: Preload on invalidation events
      • onHint: Preload on hint events
      • maxConcurrent: Max concurrent preloads

TypeScript

Full TypeScript support with generics:

interface Issue {
  id: number;
  title: string;
  status: 'open' | 'closed';
}

function IssueDetail({ id }: { id: number }) {
  // Type is inferred as Issue
  const issue = useFetch<Issue>(`/api/issues/${id}`);

  // TypeScript knows issue.title is a string
  return <h1>{issue.title}</h1>;
}

Key Benefits

  • No loading states - Suspense handles loading UI
  • Type safety - Data is always the correct type, never undefined
  • Stale-while-revalidate - Instant cache hits with background updates
  • Real-time updates - SSE integration for cache invalidation
  • Clean components - No conditional rendering for loading/error states

Requirements

  • React 18.0.0 or higher
  • @countcachula/core package

Migration from Observable Pattern

Before (using observables directly):

const [issues, setIssues] = useState<Issue[]>([]);
const [loading, setLoading] = useState(true);

useEffect(() => {
  const observable = CountCachula.fetch(request);
  const unsubscribe = observable.observe(async (response) => {
    const data = await response.json();
    setIssues(data);
    setLoading(false);
  });
  return unsubscribe;
}, []);

if (loading) return <Spinner />;
return <IssueList issues={issues} />;

After (using React hooks):

const issues = useFetch<Issue[]>('/api/issues');
return <IssueList issues={issues} />;

Much cleaner!