Cache persistence
A paged recipe list whose cache lives in localStorage. The API answers slowly on purpose; after a reload, every page
you have already opened appears at once.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { useFetch } from "@hyper-fetch/react";
import { useState } from "react";
import { client, getRecipes, persistedKeys } from "./api";
const PAGE_SIZE = 6;
const pageLoadedAt = Date.now();
const Recipes = ({ page }: { page: number }) => {
// `revalidate: false` reads fresh cached data without a request. A stale or missing entry is fetched.
const { data, error, loading, responseTimestamp } = useFetch(
getRecipes.setQueryParams({
limit: PAGE_SIZE,
skip: page * PAGE_SIZE,
// `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.
select: "name,image,cuisine,difficulty",
// A slow response, so the difference between network and cache is easy to see.
delay: 1500,
}),
{ revalidate: false },
);
if (loading && !data) return <p aria-busy="true">Loading recipes from the network...</p>;
if (error) return <p className="error">Could not load recipes: {error.message}</p>;
if (!data) return null;
const fromStorage = responseTimestamp !== null && responseTimestamp.getTime() < pageLoadedAt;
return (
<section>
<p>
{fromStorage ? <mark>Read from localStorage</mark> : "Fetched from the network"}
<small className="muted"> · saved at {responseTimestamp?.toLocaleTimeString()}</small>
</p>
<div className="recipes">
{data.recipes.map((recipe) => (
<article key={recipe.id}>
<img src={recipe.image} alt="" loading="lazy" />
<strong>{recipe.name}</strong>
<small className="muted">
{recipe.cuisine} · {recipe.difficulty}
</small>
</article>
))}
</div>
{/* Re-read on every render of this component, which happens whenever new data arrives. */}
<details>
<summary>Persisted cache keys</summary>
<ul>
{persistedKeys().map((key) => (
<li key={key}>
<code>{key}</code>
</li>
))}
</ul>
</details>
</section>
);
};
export const App = () => {
const [page, setPage] = useState(0);
const clearAndReload = async () => {
// Removes every entry from memory and from the storage.
await client.cache.clear();
window.location.reload();
};
return (
<main className="container">
<hgroup>
<h1>Recipes</h1>
<p>Open a few pages, then reload: everything you have seen shows up at once, without a request.</p>
</hgroup>
<div className="toolbar">
<div role="group">
{[0, 1, 2].map((index) => (
<button
key={index}
type="button"
className={index === page ? undefined : "outline secondary"}
onClick={() => setPage(index)}
>
Page {index + 1}
</button>
))}
</div>
<div role="group">
<button type="button" className="secondary" onClick={() => window.location.reload()}>
Reload page
</button>
<button type="button" className="outline" onClick={clearAndReload}>
Clear persisted cache
</button>
</div>
</div>
<Recipes page={page} />
</main>
);
};
npx tiged BetterTyped/hyper-fetch/examples/react/cache-persistence hyper-fetch-react-cache-persistence && cd hyper-fetch-react-cache-persistence && npm install && npm run devCache({ storage })replaces the in-memoryMapwith a storage backed bylocalStorage.staleTimeandcacheTimedecide how long persisted data is used without a request and how long it is kept.revalidate: falseletsuseFetchtrust fresh cached data instead of fetching on every mount.cache.clear()removes the entries from memory and from the storage.
How it works
-
api.tsimplementsCacheStorageType(get,set,keys,delete,clear) on top oflocalStorage, with a key prefix so it only touches its own entries. Values go throughJSON.stringify, so cached data must be JSON-serializable. -
The client receives it through the
cacheoption:cache: () => new Cache({ storage: localStorageCache, version: "1" }),Entries saved under another
versionare ignored and removed, so changing it is a safe way to drop old data after a release. -
The storage is synchronous, so the first render after a reload already reads the saved response from it. Async storages such as IndexedDB go in
lazyStorageinstead and update the hooks once the read finishes. -
getRecipessetsstaleTime: Time.MINandcacheTime: Time.DAY. With the defaultcacheTimeof 5 minutes, the garbage collector would remove persisted entries soon after the app starts. -
useFetch(..., { revalidate: false })sends no request while the data is fresh. The label comparesresponseTimestampwith the page load time to show whether the data came from storage or from the network. -
Clear persisted cache calls
client.cache.clear()and reloads, so every page is fetched again.
- Apps that people open often, where the last data is better than a spinner.
- Slow or metered networks.
- Together with the offline queue, for apps that start without a connection.
Related
A five-method storage object passed to new Cache({ storage }) persists every cached response. staleTime, cacheTime
and revalidate decide when that data is shown as is and when it is fetched again.
