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

cachefirst

v0.6.0

Published

CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.

Downloads

99

Readme

CacheFirst

CacheFirst is a lightweight, isomorphic JavaScript library that provides a cache-first strategy for fetching and storing data in web applications. It supports caching in localStorage, IndexedDB, and in-memory, making it ideal for fast, reliable data access and offline experiences.

Current version: 0.6.0

Features

  • Cache-first fetch API for JSON, Blob, Text, and ArrayBuffer
  • Automatic cache key generation (SHA-256 of request arguments)
  • Custom cache keys (cacheKey option)
  • Time-To-Live (TTL) expiration per request (ttl option in ms)
  • Automatic cache update only when payload changes (content hash compare)
  • Multi-tier storage: in-memory (SSR), localStorage (small payloads), IndexedDB (larger)
  • Registry-based bulk clear APIs (clear, clearAll)
  • Pluggable error handler (setCatch / .catch() chain)
  • Zero dependencies, UMD build (works with ESM/CJS/global)
  • TypeScript definitions included

Installation

CDN

<script src="https://unpkg.com/cachefirst@latest/cachefirst.min.js"></script>

NPM

npm install cachefirst

Basic Usage

CacheFirst.fetch('https://api.example.com/data')
  .json((data, isFresh, response) => {
    console.log('Data', data);
    console.log('Fresh from network?', isFresh);
  })
  .catch(err => console.error(err));

isFresh is false for an immediate cached emission (if present) and true when the network response (if different) arrives.

Advanced Usage

Custom Cache Key

CacheFirst.fetch('https://api.example.com/user/123', { method: 'GET' }, { cacheKey: 'user:123' })
  .json(cb);

TTL (Expire After 5 Minutes)

CacheFirst.fetch('https://api.example.com/config', { method: 'GET' }, { ttl: 5 * 60 * 1000 })
  .json(cb);

Combined Options (second param RequestInit, third param cache options)

CacheFirst.fetch('https://api.example.com/list', { headers: { 'X-Feature': 'A' } }, { ttl: 10000, cacheKey: 'list:A' })
  .json(cb);

Manual Cache Clearing

await CacheFirst.clear('user:123'); // remove a specific entry
await CacheFirst.clearAll();        // purge everything managed by CacheFirst

Global Error Handler

CacheFirst.setCatch(err => console.warn('CacheFirst error', err));

Or per-call chain:

CacheFirst.fetch(url).json(cb).catch(err => console.error(err));

API Reference

fetch(input, [requestInit], [cacheOptions])

cacheOptions supports:

  • cacheKey?: string custom key overriding auto-hash
  • ttl?: number milliseconds-to-live; expired entries are purged lazily

Returns a chain object with:

  • .json(handler)
  • .text(handler)
  • .blob(handler)
  • .arrayBuffer(handler)
  • .catch(handler) set error callback

Handler signature:

(data: any, isFresh: boolean, response?: Response) => void

clear(key: string): Promise

Remove a specific cached entry (all tiers).

clearAll(): Promise

Remove all entries created via CacheFirst.

setCatch(fn: (err: any) => void): void

Set a global error handler.

How It Works

  1. Derives (or uses provided) cache key.
  2. Emits cached data (if present & not expired) immediately.
  3. Performs a network fetch with a cache-busting query param (userLocalTime).
  4. Clones response; reads body twice safely.
  5. Hashes body with the response reader function name to detect meaningful changes.
  6. Updates cache only if content hash differs.
  7. Emits fresh data if changed.

TypeScript

Basic typings are included (see index.d.ts).

Sample

See sample.html for a runnable demonstration.

Roadmap Ideas

  • Stale-while-revalidate mode toggle
  • Batch preloading
  • Size-based eviction policy
  • Optional compression

License

MIT

Contributing

PRs and issues welcome.

Author

Lakshminathan S