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

@lxsmnsyc/react-suspense-cache

v0.1.0

Published

React library for creating Suspense-ful cached resources

Readme

react-suspense-cache

React library for creating Suspense-ful cached resources

Features

  • Reactive - Data-fetching components that reads from resources can automatically re-render to update from the resource's data. This is useful if the data being served has been updated in the background.
  • Caching Strategies - Inspired by the Offline Cookbook and Google's Workbox, react-suspense-cache implements the caching strategies for the Suspense resources. 7 caching strategies are already built-out-of-the-box.
  • Plugins - Caching strategies can receive plugins that allows additional business logic on how the data is being handled ignorely. An example is imposing an expiration policy through the built-in ExpirationPlugin.

Install

yarn add @lxsmnsyc/react-suspense-cache

Usage

To create a resource, you must import and call the function createResource which receives a config object:

import { createResource } from '@lxsmnsyc/react-suspense-cache';

const myResource = createResource({ ... });

The resource created has three functions:

  • read - a function that can receive a parameter and return data. This function shall be called by the data-fetching components.
  • mutate - receives a data and multiple values. Replaces the cached data with the given data. All data-fetching components of similarly received arguments (except the data) will be re-rendered.
  • trigger - triggers a refetch for the specific resource and re-renders all data-fetching component of similar arguments.

The config object has the options:

  • keyFactory - a function that receives the parameters received by the Resource's methods, and returns a string which is used as a key for caching.
  • fetcher - a function that provides the data. Receives the same parameters.
  • cacheName - optional. Used for the global cache as a way to separate it from the rest of the resources (assuming these resources have no cache names.).
  • strategy - optional. Implements the resource-to-cache data policy. If no strategy is provided, the data is stored resource-level.
  • revalidateOnVisibility - optional. Whenever the page becomes visible, all data-fetching components will perform a data revalidation. Defaults to false.
  • updateOnVisibility - optional. Whenever the page becomes visible, all data-fetching components will re-render. Defaults to false.

Example

Here's a simple resource that fetches data from the Dog API with the given kind. The resource employs the stale-while-revalidate strategy with an data expiration time of 10 seconds.

const randomDog = createResource({
  /**
   * Generate key for the cache
   */
  keyFactory(kind) {
    return `https://dog.ceo/api/breed/${kind}/images/random`;
  },
  /**
   * Data provider
   */
  async fetcher(kind) {
    const response = await fetch(`https://dog.ceo/api/breed/${kind}/images/random`);
    const json = await response.json();
    return json.message;
  },
  /**
   * Use a stale-while-revalidate strategy with a data expiration
   * of 10 seconds.
   */
  strategy: new strategies.StaleWhileRevalidate({
    plugins: [
      new plugins.ExpirationPlugin(10),
    ],
  }),
  /**
   * Revalidate our components every time the page goes visible again.
   */
  revalidateOnVisibility: true,
});

Strategies

Strategies (aka ResourceHandler) are a way to handle resource-to-cache data flow. The library has 7 strategies built out of the box, mainly inspired by the Offline Cookbook:

  • CatcherFetcherRace: Fetches the data from both the data fetcher and the cache at the same time, and presents the data from whichever comes first.
  • CacheFirst: Tries to fetches the data from the cache first. If the cached data does not exist, resorts to data fetching instead (no caching).
  • CacheOnly: Fetches data from the cache. If the data does not exist, throws an error.
  • CacheOrFetcher: Similar to CacheFirst, but the fetcher caches data after resolving.
  • FetcherFirst: Fetches data from the fetcher before resolving to cache. This strategy accepts a timeout in as a way to mark the fetcher with a time restrain (treating it as a failure.)
  • FetcherOnly: Fetches data from the fetcher.
  • StaleWhileRevalidate: Presents a stale data from the cache, while asynchronously fetching the new data in the background. The newly fetched data is then cached.

This strategies can be optionally provided to the resources. If the strategies are not provided, the resources will perform a strategy similar to FetcherOnly.

Plugins

Plugins extends the capability of the Strategies. Plugins allows to receive, process, and return requests and responses. Strategies can act upon these changes and handles them.

The package has 2 plugins:

  • ExpirationPlugin: receives an amount in seconds. Sets the cache age to that amount and allows cache revalidation to occur whenever the cache expires.
  • SuccessOnlyPlugin: returns undefined from failed responses.

How does it work?

A traditional Suspense resource has a single source of data, whose data is kept at the entire runtime. That's one layer of data flow. This library has 4 layers of data flow:

  • Global Cache: a runtime cache that stores every single data from every single resource.
  • Strategy: Handles the resource-to-cache logic, this includes cache loading and data fetching.
  • Resource: provides the data-fetching logic to the strategy, keeps the Promise instance for the Suspense components.
  • Components: reads the data from the resource, and can reactively update when necessary.

Similar libraries