cachefirst v0.5.0
CacheFirst
CacheFirst is an ismorphic caching library that offers fluent APIs for caching JSON, Blob, Text, and ArrayBuffer response data.
Key Features
- The response data is stored in in-memory or the browser cache.
- The storage mechanism can be either in-memory or localStorage or IndexedDB, determined based on the platform ,response-type and length.
- The callback function is immediately called if data is available in the cache.
- The callback is invoked again only if the actual response differs from the cached response.
The callback receives three parameters:
i. Blob, String, Object, ArrayBuffer - Type-converted response body based on the invoked method.
ii. Boolean - True if the network response is different from the cached response.
iii. Response - Wrapper of the original response, allowing operations with the original response.
SHA-256 algorithm is used to generate hash from request and response for integrity
- A catch callback is provided to handle errors effectively.
Installation
To use as Module:
npm install cachefirst
Directly in HTML:
<script crossorigin src="https://unpkg.com/cachefirst@latest/cachefirst.min.js"></script>
Usage
import CacheFirst from 'cachefirst';
or
<script crossorigin src="https://unpkg.com/cachefirst@latest/cachefirst.min.js"></script>
CacheFirst.fetch("http://localhost:3000/v1/text")
.text((data) => {
document.body.innerHTML += "<br><br>" + data;
})
.catch(error => {
console.log(error);
});
CacheFirst.fetch("http://localhost:3000/v1/json")
.json((data, hasDataModified, originalResponse) => {
if (hasDataModified || originalResponse.status == 200)
document.body.innerHTML += "<br><br>" + JSON.stringify(data);
})
.catch(error => {
console.log(error);
});
CacheFirst.fetch("http://localhost:3000/v1/blob")
.blob((data, hasDataModified) => {
blobToBase64(data).then(str => document.body.innerHTML += "<br><br>" + str);
})
.catch(error => {
console.log(error);
});
CacheFirst.fetch("http://localhost:3000/v1/arrayBuffer")
.arrayBuffer((data, hasDataModified) => {
document.body.innerHTML += "<br><br>" + new Uint8Array(data);
});
To-do
- tests
- check cache size and implement fallback mechanism
- expose api's to make config changes
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago