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

browser-cache-async

v1.0.6

Published

The browser-cache-async package provides a highly customizable, asynchronous caching system for client-side data management. Leveraging the power of the browser's IndexedDB, it enables efficient storage and retrieval of API responses, significantly reduci

Readme

Browser Cache Async

The browser-cache-async package provides a highly customizable, asynchronous caching system for client-side data management. Leveraging the power of the browser's IndexedDB, it enables efficient storage and retrieval of API responses, significantly reducing network latency and improving application performance.

With flexible configuration options for cache lifecycles, expiration strategies, and data management policies, browser-cache-async is the ideal solution for developers looking to optimize web applications, enhance offline capabilities, and deliver a smoother user experience.

Getting Started

Install the package:

npm i -S browser-cache-async

Usage

Cache the result of a fetch request:

import { BrowserCache } from 'browser-cache-async';
import { IProduct } from './types.js';

const cache = new BrowserCache<IProduct>('products');

// retrieve and cache the product. If revalidate is not provided, the data becomes stale after 24 hours
const id = 1;
const product = await cache.run({
  id,
  query: async () => {
    const res = await fetch(`https://fakestoreapi.com/products/${id}`);
    return res.json();
  }
});
// {
//   id: 1,
//   title: '...',
//   price: '...',
//   category: '...',
//   description: '...',
//   image: '...'
// }

Cache an article for 2 weeks if it has been published:

import { BrowserCache } from 'browser-cache-async';
import { BackendService } from './backend.js';
import { IArticle } from './types.js';

const cache = new BrowserCache<IArticle>('articles');

const id = 'db6d9d01-8d67-4765-8baa-2210cbc0470e';
const article = await cache.run({
  id,
  query: async () => BackendService.getArticle(id),
  cacheIf: (id: IRecordID, data: IArticle) => data.isDraft === false,
  revalidate: '2 weeks'
});
// {
//   id: 'db6d9d01-8d67-4765-8baa-2210cbc0470e',
//   heading: '...',
//   subHeading: '...',
//   content: '...',
//   isDraft: false
// }

// if the article is updated or removed, trigger a revalidation manually
await cache.revalidate(id);

Types

The identifier used to manage records. The store behaves differently based on the type:

  • undefined: the data will be stored at the root of the store
  • string | number: the value will be coerced into a string and can be used to locate the data
type IRecordID = undefined | string | number;

Note: this type is exposed by the browser-keyval-stores package.

The template literal types that prevents developers from passing invalid strings to the ms function.

type IUnit =
| 'Years'
| 'Year'
| 'Yrs'
| 'Yr'
| 'Y'
| 'Weeks'
| 'Week'
| 'W'
| 'Days'
| 'Day'
| 'D'
| 'Hours'
| 'Hour'
| 'Hrs'
| 'Hr'
| 'H'
| 'Minutes'
| 'Minute'
| 'Mins'
| 'Min'
| 'M'
| 'Seconds'
| 'Second'
| 'Secs'
| 'Sec'
| 's'
| 'Milliseconds'
| 'Millisecond'
| 'Msecs'
| 'Msec'
| 'Ms';

type IUnitAnyCase = IUnit | Uppercase<IUnit> | Lowercase<IUnit>;

type IStringValue =
| `${number}`
| `${number}${IUnitAnyCase}`
| `${number} ${IUnitAnyCase}`;

Note: this type is exposed by the ms package`.

Utility type to indicate the function that will be invoked to evaluate if the data should be cached.

type ICacheIfFn<T> =
  ((id: IRecordID, data: T) => Promise<boolean>) | ((id: IRecordID, data: T) => boolean);

Object in charge of controlling how the query is executed and cached.

import { IRecordID } from 'browser-keyval-stores';

type IQueryOptions<T> = {
  // the record's identifier
  id?: IRecordID;

  // the function that will be invoked to retrieve the data
  query: () => Promise<T>;

  // the function that will be invoked to evaluate if the data should be cached. If not provided,
  // the data will always be cached.
  cacheIf?: ICacheIfFn | boolean;

  // the number of milliseconds the data will be fresh for before becoming stale. If not provided,
  // the data will become stale after 1 day.
  revalidate?: IStringValue | number;
};

// the result of processing the query options object passed by the developer
type IProcessedQueryOptions<T> = IQueryOptions<T> & { revalidate: number };

Object in charge of managing the caching of data in the browser.

interface IBrowserCache<T> {
  // properties
  // ...

  // actions
  run: (options: IQueryOptions<T>) => Promise<T>;
  revalidate(id?: IRecordID): Promise<void>;
}

Built With

  • TypeScript

Tests

# integration tests
npm run test:integration

# unit tests
npm run test:unit

License

MIT