{"_id":"cachefirst","_rev":"28-b300b770c40a6d399fd66773d5573bcc","name":"cachefirst","dist-tags":{"latest":"0.6.0"},"versions":{"0.1.0":{"name":"cachefirst","version":"0.1.0","keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"ISC","_id":"cachefirst@0.1.0","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"homepage":"https://github.com/lakshminathan/CacheFirst#readme","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"dist":{"shasum":"b3210154814b9a23bd99fdf150afeeb43fe6fddc","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.1.0.tgz","fileCount":5,"integrity":"sha512-e7xHTDd16KPXwND9IJ5DzlToXWwA7BhNx3MBAEXjFtDLv0VX4iizlgRPWMY8rxZcengsxDhx+spEQU/ED0/9tA==","signatures":[{"sig":"MEYCIQDseOtuKsDpCA8RLlJeDZdsY4PSYimaW+3DpcHEtME0awIhAO63d4GG/+bFm+n3evWVymhYv27Finm9g63IXyg3n/iP","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":12436},"main":"cachefirst.js","gitHead":"e4d21d9d7363d9462c93c82f32f0dbbd1a589497","scripts":{"test":"echo \"Error: no test specified\""},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"repository":{"url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git","type":"git"},"_npmVersion":"10.2.4","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cachefirst_0.1.0_1703837981006_0.14802907200131732","host":"s3://npm-registry-packages"}},"0.2.0":{"name":"cachefirst","version":"0.2.0","keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"ISC","_id":"cachefirst@0.2.0","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"homepage":"https://github.com/lakshminathan/CacheFirst#readme","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"dist":{"shasum":"d4d04edad7dc3e8df8a9fd7affd82f2cacafcc6f","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.2.0.tgz","fileCount":5,"integrity":"sha512-mc1MOrbBocl05diuEUxJ23ycIJ1H8jx86rk5yA5h2T+bhBo0GwH3y8Kqlu4+NNGc1cn8LIfWGY9/O7ujeAyQyw==","signatures":[{"sig":"MEQCIEyrR6lXazkRnHhKqdPGcHuyvqLW1xkXO0HLQV5eQTCWAiBQaMRrW1uB2TOlM8sZ+DpU88JIuJAy4/jXbKqj3P2S8A==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":12814},"main":"cachefirst.js","gitHead":"c2553c8608e14a8cb20453835619bdd0e14a8410","scripts":{"test":"echo \"Error: no test specified\""},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"repository":{"url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git","type":"git"},"_npmVersion":"10.2.4","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cachefirst_0.2.0_1703855591091_0.489135545697593","host":"s3://npm-registry-packages"}},"0.3.0":{"name":"cachefirst","version":"0.3.0","keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"ISC","_id":"cachefirst@0.3.0","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"homepage":"https://github.com/lakshminathan/CacheFirst#readme","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"dist":{"shasum":"571d985515fbb860928a9f93756d3e98541a92b2","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.3.0.tgz","fileCount":6,"integrity":"sha512-aa931GEOCYagrUe0dnBDp/mOKrf+P/RtL/1OjKr0kjMnERc+u8MZe/kM9oiQgVU4HVPzHnVPPn4UslvKX256Pw==","signatures":[{"sig":"MEUCIBcrArGBfVEPLl/YsfJ6CZwq1NMN5BBPySK7DoIQ6TBLAiEAlskCUD0JKtwViqAhjNnkcZbOlDukKPWLT988s24ojng=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15712},"main":"cachefirst.js","gitHead":"94912d76184369b91b4e06b85cf76226b65a75c4","scripts":{"test":"echo \"Error: no test specified\""},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"repository":{"url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git","type":"git"},"_npmVersion":"10.2.4","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cachefirst_0.3.0_1703859392737_0.839271437673291","host":"s3://npm-registry-packages"}},"0.4.0":{"name":"cachefirst","version":"0.4.0","keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"ISC","_id":"cachefirst@0.4.0","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"homepage":"https://github.com/lakshminathan/CacheFirst#readme","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"dist":{"shasum":"18ad26e78a0cc2ebe116c46ce5bcf8c14dc9f2a5","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.4.0.tgz","fileCount":6,"integrity":"sha512-WFbKpGjLF385Cs1hyJvvDcL/udIAsBmT0RrOetcNMVvDrXs3fmS653vpbvTl/fEqslCCweH7ASDsbalCso3W9w==","signatures":[{"sig":"MEUCIQCxS1E3W3bXGccV4y3WwYgG5P8UkS1mol2w3pY8UOj5PQIgeTLjANYHfq3fY1PipFAjfn4gSnlzNAIcczzw8Bzl/7E=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15716},"main":"cachefirst.js","gitHead":"d4e0b6b0f3d8a782786a4e7c81e7df4bed23d9c2","scripts":{"test":"echo \"Error: no test specified\""},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"repository":{"url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git","type":"git"},"_npmVersion":"10.2.4","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cachefirst_0.4.0_1703859525610_0.4294590767476161","host":"s3://npm-registry-packages"}},"0.5.0":{"name":"cachefirst","version":"0.5.0","keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"ISC","_id":"cachefirst@0.5.0","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"homepage":"https://github.com/lakshminathan/CacheFirst#readme","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"dist":{"shasum":"ac47c03981a75c11700b7a17bf0054ba9d2eea3b","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.5.0.tgz","fileCount":6,"integrity":"sha512-Rfw7M3rE+X8ThMbXFnQvCDLw8vVhxiNvwShjlUXnT9IxHesH5nt5GU838YdB45ZIrVuxjWZUUmCJikdGMW0uYQ==","signatures":[{"sig":"MEUCIQC42x3gB4PYE2LC/zCNEi3oR0AchNNoY+9pGng8vQbvxwIgVSOXUJA+6EGSORtb5kS2SikFxmtbZ2h3rzauIo9VCXI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15770},"main":"cachefirst.js","gitHead":"8895a55eac9385b7a953bb431cb2c584fa02951c","scripts":{"test":"echo \"Error: no test specified\""},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"repository":{"url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git","type":"git"},"_npmVersion":"10.2.4","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cachefirst_0.5.0_1703859591278_0.04547969942640617","host":"s3://npm-registry-packages"}},"0.6.0":{"name":"cachefirst","version":"0.6.0","description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","main":"cachefirst.js","types":"index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git"},"keywords":["fetch","response","cache","localstorage","indexedDB"],"author":{"name":"Lakshminathan S"},"license":"MIT","bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"homepage":"https://github.com/lakshminathan/CacheFirst#readme","scripts":{"test":"echo \"No tests yet.\"","build":"uglifyjs cachefirst.js -o cachefirst.min.js --compress --mangle","prepublishOnly":"npm run build"},"devDependencies":{"uglify-js":"^3.19.0"},"gitHead":"e3acc43a0a2207c7d85569ca2c063a7272e8d937","_id":"cachefirst@0.6.0","_nodeVersion":"16.20.2","_npmVersion":"8.19.4","dist":{"integrity":"sha512-rIv44WZoLbiAouD05RM6YUvs9aisgxgUjyf0PWJL0phgPO1WBchwTymVB5VtCBAdxcy/i4Upjkf2TglCH4gLag==","shasum":"35d01b1712471d74c9d43fad760d8645179f6a1c","tarball":"https://registry.npmjs.org/cachefirst/-/cachefirst-0.6.0.tgz","fileCount":6,"unpackedSize":20509,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIFC6GSagR6tbnSiQDGf0JEqpwnQjLkrniUuVvffQkr7bAiEAlpj02eJTuUIJSUA0fB3kBfLmISGVovSzZTcdo50nKpg="}]},"_npmUser":{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"},"directories":{},"maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cachefirst_0.6.0_1755982804823_0.22067285769367762"},"_hasShrinkwrap":false}},"time":{"created":"2023-12-28T14:09:34.354Z","modified":"2025-08-23T21:00:05.222Z","0.0.1-alpha":"2023-12-28T14:09:34.545Z","0.0.2-alpha":"2023-12-28T14:36:07.635Z","0.0.3-alpha":"2023-12-28T16:25:25.001Z","0.0.4-alpha":"2023-12-28T16:37:38.094Z","0.0.5-alpha":"2023-12-28T18:59:13.717Z","0.0.6-alpha":"2023-12-28T19:46:10.439Z","0.0.7-alpha":"2023-12-28T19:57:26.214Z","0.0.8-alpha":"2023-12-28T20:10:04.525Z","0.0.9-alpha":"2023-12-28T20:19:25.018Z","0.0.10-alpha":"2023-12-29T05:42:44.122Z","0.0.11-alpha":"2023-12-29T08:14:36.010Z","0.1.0":"2023-12-29T08:19:41.159Z","0.2.0":"2023-12-29T13:13:11.274Z","0.3.0":"2023-12-29T14:16:32.913Z","0.4.0":"2023-12-29T14:18:45.772Z","0.5.0":"2023-12-29T14:19:51.492Z","0.6.0":"2025-08-23T21:00:05.021Z"},"bugs":{"url":"https://github.com/lakshminathan/CacheFirst/issues"},"author":{"name":"Lakshminathan S"},"license":"MIT","homepage":"https://github.com/lakshminathan/CacheFirst#readme","keywords":["fetch","response","cache","localstorage","indexedDB"],"repository":{"type":"git","url":"git+ssh://git@github.com/lakshminathan/CacheFirst.git"},"description":"CacheFirst is a ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer data.","maintainers":[{"name":"lakshminathan","email":"lakshminathanlaky@gmail.com"}],"readme":"# CacheFirst\n\nCacheFirst 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.\n\nCurrent version: 0.6.0\n\n## Features\n- Cache-first fetch API for JSON, Blob, Text, and ArrayBuffer\n- Automatic cache key generation (SHA-256 of request arguments)\n- Custom cache keys (`cacheKey` option)\n- Time-To-Live (TTL) expiration per request (`ttl` option in ms)\n- Automatic cache update only when payload changes (content hash compare)\n- Multi-tier storage: in-memory (SSR), localStorage (small payloads), IndexedDB (larger)\n- Registry-based bulk clear APIs (`clear`, `clearAll`)\n- Pluggable error handler (`setCatch` / `.catch()` chain)\n- Zero dependencies, UMD build (works with ESM/CJS/global)\n- TypeScript definitions included\n\n## Installation\n### CDN\n```html\n<script src=\"https://unpkg.com/cachefirst@latest/cachefirst.min.js\"></script>\n```\n### NPM\n```bash\nnpm install cachefirst\n```\n\n## Basic Usage\n```js\nCacheFirst.fetch('https://api.example.com/data')\n  .json((data, isFresh, response) => {\n    console.log('Data', data);\n    console.log('Fresh from network?', isFresh);\n  })\n  .catch(err => console.error(err));\n```\n`isFresh` is `false` for an immediate cached emission (if present) and `true` when the network response (if different) arrives.\n\n## Advanced Usage\n### Custom Cache Key\n```js\nCacheFirst.fetch('https://api.example.com/user/123', { method: 'GET' }, { cacheKey: 'user:123' })\n  .json(cb);\n```\n### TTL (Expire After 5 Minutes)\n```js\nCacheFirst.fetch('https://api.example.com/config', { method: 'GET' }, { ttl: 5 * 60 * 1000 })\n  .json(cb);\n```\n### Combined Options (second param RequestInit, third param cache options)\n```js\nCacheFirst.fetch('https://api.example.com/list', { headers: { 'X-Feature': 'A' } }, { ttl: 10000, cacheKey: 'list:A' })\n  .json(cb);\n```\n### Manual Cache Clearing\n```js\nawait CacheFirst.clear('user:123'); // remove a specific entry\nawait CacheFirst.clearAll();        // purge everything managed by CacheFirst\n```\n### Global Error Handler\n```js\nCacheFirst.setCatch(err => console.warn('CacheFirst error', err));\n```\nOr per-call chain:\n```js\nCacheFirst.fetch(url).json(cb).catch(err => console.error(err));\n```\n\n## API Reference\n### fetch(input, [requestInit], [cacheOptions])\n`cacheOptions` supports:\n- `cacheKey?: string` custom key overriding auto-hash\n- `ttl?: number` milliseconds-to-live; expired entries are purged lazily\n\nReturns a chain object with:\n- `.json(handler)`\n- `.text(handler)`\n- `.blob(handler)`\n- `.arrayBuffer(handler)`\n- `.catch(handler)` set error callback\n\nHandler signature:\n```ts\n(data: any, isFresh: boolean, response?: Response) => void\n```\n\n### clear(key: string): Promise<void>\nRemove a specific cached entry (all tiers).\n\n### clearAll(): Promise<void>\nRemove all entries created via CacheFirst.\n\n### setCatch(fn: (err: any) => void): void\nSet a global error handler.\n\n## How It Works\n1. Derives (or uses provided) cache key.\n2. Emits cached data (if present & not expired) immediately.\n3. Performs a network fetch with a cache-busting query param (`userLocalTime`).\n4. Clones response; reads body twice safely.\n5. Hashes body with the response reader function name to detect meaningful changes.\n6. Updates cache only if content hash differs.\n7. Emits fresh data if changed.\n\n## TypeScript\nBasic typings are included (see `index.d.ts`).\n\n## Sample\nSee `sample.html` for a runnable demonstration.\n\n## Roadmap Ideas\n- Stale-while-revalidate mode toggle\n- Batch preloading\n- Size-based eviction policy\n- Optional compression\n\n## License\nMIT\n\n## Contributing\nPRs and issues welcome.\n\n## Author\nLakshminathan S\n","readmeFilename":"README.md"}