SWR (Stale-While-Revalidate) is a React hooks library for data fetching that provides a simpler alternative to React Query with built-in caching and revalidation strategies.
- Stale-While-Revalidate: Cache-first strategy
- Real-time: Auto-revalidate on focus
- Smart Caching: Automatic cache management
- Lightweight: Minimal bundle size
- Suspense Support: React Suspense compatible
- TypeScript: Full type support
- Global Configuration: App-wide settings
- Middleware: Custom data transformations
- Data fetching hook (useSWR)
- Automatic revalidation
- Focus tracking
- Interval polling
- Offline support
- Error retry
- Dedupe requests
- Local mutation
- Conditional fetching
- Dependent requests
- revalidateOnFocus: Revalidate on window focus
- revalidateOnReconnect: Revalidate on reconnect
- revalidateIfStale: Revalidate if stale
- shouldRetryOnError: Auto-retry on error
- dedupingInterval: Dedup window
- focusThrottleInterval: Focus throttle
- errorRetryCount: Retry count
- errorRetryInterval: Retry interval
const { data, error, isLoading } = useSWR('/api/user', fetcher)- API Data Fetching
- Real-time Updates
- Caching Data
- Offline Support
- Auto-revalidation
- Polling
- Dependent Queries
- Simpler API
- Better UX
- Lighter weight
- Great defaults
- Vercel integration
Free and open-source under the MIT license.