Smart, cached, resilient, TypeScript-first data fetching for React
Smart data fetching for React with memory + IndexedDB caching, SWR, retries, token refresh, schema validation, predictive prefetching, and built-in DevTools.
use-fetch-smart is a powerful data-fetching library for React engineered for real-world apps.
It provides:
- Smart caching (memory + IndexedDB)
- SWR (stale-while-revalidate) support
- Request deduplication
- Automatic retry & exponential backoff
- Full mutation support (POST, PUT, DELETE)
- Token auto-refresh & request replay
- Predictive prefetching engine
- Schema validation (Zod / Yup / Valibot / custom)
- Built-in Developer Tools
const { data, loading, error } = useGetSmart("/users");npm install use-fetch-smart
# or
yarn add use-fetch-smartWrap your app with the provider:
import { FetchSmartProvider, FetchSmartDevtools } from "use-fetch-smart";
const refreshToken = async () => {
const res = await fetch("/auth/refresh");
if (!res.ok) return null;
return (await res.json()).token;
};
export default function Root() {
return (
<FetchSmartProvider
config={{
baseURL: "http://localhost:4000",
retryLimit: 3,
refreshToken,
}}
>
<App />
<FetchSmartDevtools /> {/* Optional in dev */}
</FetchSmartProvider>
);
}const { data, loading, error } = useGetSmart("/users", {
cacheTimeMs: 60000,
swr: true,
});const { mutate } = usePostSmart("/login");
mutate({ email, password });usePutSmart("/update-user");
useDeleteSmart("/remove-user");useGetSmart("/products?page=1", {
prefetchNext: (data) => [
{ url: `/products?page=${data.nextPage}` },
{ url: `/products/summary`, ttlMs: 5000 },
],
});import { UserSchema } from "./schemas";
const { data } = useGetSmart("/profile", {
schema: UserSchema,
schemaMode: "error",
});Supports: Zod, Yup, Valibot, custom validators.
| Hook | Method | Purpose |
|---|---|---|
useGetSmart |
GET | Fetch with cache, SWR, dedupe |
usePostSmart |
POST | Mutations |
usePutSmart |
PUT | Mutations |
useDeleteSmart |
DELETE | Mutations |
useSmart |
Any | Low-level full control |
FetchSmartProvider |
Provider | Global config |
FetchSmartDevtools |
Component | Inspect cache, TTL, dedupe state |
- Checks memory cache
- If expired → checks IndexedDB
- If miss → HTTP request with retry + dedupe
- Validates schema
- Updates caches
- Triggers SWR or prefetch (if enabled)
| Feature | use-fetch-smart | SWR | TanStack Query |
|---|---|---|---|
| Token auto-refresh | ✅ | ❌ | ❌ |
| Request replay after refresh | ✅ | ❌ | ❌ |
| IndexedDB cache | ✅ | ❌ | |
| Predictive prefetching | ✅ | ❌ | ❌ |
| Schema validation | ✅ | ||
| Built-in devtools | ✅ | ||
| Boilerplate required | Minimal | Medium | High |
const { error, retry } = useGetSmart("/data", { retryLimit: 3 });
if (error)
return (
<>
<p>Failed to load data.</p>
<button onClick={retry}>Retry</button>
</>
);<FetchSmartDevtools />Shows:
- cache entries
- TTL timers
- request dedupe keys
- prefetch queue
- SWR events
/src
/core
/hooks
/cache
/utils
/schemas
/examples
cd examples/backend && npm install && node server.js
cd ../frontend && npm install && npm run dev Pull requests and suggestions are welcome!
If you like this package, please star the repo — it helps a lot.
MIT License © 2025 Zaid Shaikh