GithubHelp home page GithubHelp logo

lxsmnsyc / react-suspense-cache Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 1.12 MB

React library for creating Suspense-ful cached resources

Home Page: https://lxsmnsyc.github.io/react-suspense-cache/

License: MIT License

TypeScript 98.02% JavaScript 1.55% HTML 0.43%
react suspense data-fetching cache

react-suspense-cache's Introduction

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

react-suspense-cache's People

Contributors

dependabot[bot] avatar lxsmnsyc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.