# SWR — card for AI agents

React hooks library for data fetching with caching, revalidation and real-time updates.

Category: State and data fetching · npm package `swr` · latest version 2.5.1 (checked 2026-11-01)
Install: `npm install swr`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## SWR
- Call `useSWR`, `useSWRInfinite` and `useSWRMutation` only in Client Components (`'use client'`). In Server Components, import only `SWRConfig`, `preload` and `unstable_serialize`.
- Use one consistent key per resource, normally the API URL. The same data must always get the same key, and different data must never share a key.
- To prefetch on the server, call `preload(key, fetcher)` and pass the merged results to ``. Never pass `cacheData` to `useSWR`. This requires SWR 2.5.0-beta.1+ and is experimental.
- When building `cacheData` by hand with array or function keys, serialize them with `unstable_serialize`.
- Show spinners from `isLoading`, which is only true with no loaded data. Use `isValidating` for background refreshes, such as disabling a Retry button.
- With `useSWRInfinite`, return `null` from `getKey` when the previous page is empty, to stop fetching.
- Retries are on by default (`shouldRetryOnError = true`). Set it to `false` if the UI offers its own retry via `mutate()`.

## Docs written for agents
- [llms.txt (index of the docs)](https://swr.vercel.app/llms.txt)

## What it is
- SWR is a React Hooks library for data fetching from Vercel. The README says it was created by the team behind Next.js. The package is `swr` on npm and is MIT licensed. The GitHub repository was created on 2019-10-28, and the latest listed release is v2.5.1. The name comes from `stale-while-revalidate`, the cache invalidation strategy from HTTP RFC 5861.
- The core API is `useSWR(key, fetcher, options)`. The key identifies the request and is usually the API URL. The fetcher is any async function that takes the key and returns data. The hook returns `data`, `error`, `isLoading`, `isValidating` and `mutate`. The cached value comes back first, a request goes out, and the component re-renders with fresh data. Revalidation on focus and on reconnect, deduplication and error retry are on by default.
- The llms.txt file names its audience as React and Next.js developers. Typical jobs are fetching, caching and revalidating remote data, plus pagination, mutations and optimistic UI. Separate entry points cover infinite lists (`swr/infinite`) and mutations (`swr/mutation`).

## Caveats
- Hooks are client-only: `useSWR`, `useSWRInfinite` and the mutation hook cannot be imported in React Server Components. Only `SWRConfig`, `preload` and the key serialization APIs work there.
- Server prefetch is experimental: The `preload` + `cacheData` pattern needs a recent version and is flagged as experimental in the docs.
- cacheData goes on SWRConfig only: You cannot pass `cacheData` to an individual hook. Array or function keys also have to be serialized with `unstable_serialize` when you build `cacheData` by hand.
- Cache keys need a team convention: incident.io found that hand-written keys drift. Different keys for the same data lose the shared cache. The same key for different data causes what the author calls really weird bugs. Their fix was a typed wrapper that builds the key from the API name and request.
- Errors are retried unless you turn it off: `shouldRetryOnError` defaults to `true`. The official error-handling example sets it to `false` and retries manually with `mutate()`.

## Numbers (AgentGid data)
- Gid Score: 77.3 (#148 of all tools, 4 of 53 in its category)
- Used in 51 public projects set up for AI coding agents (0.8%)
- Downloads last week: 28,540,199 (as of 2026-11-24), +44.9% over 4 weeks
- GitHub stars: 32,493 (as of 2026-11-24)
- Last push: 2026-10-16

## Often used with
Next.js 88.2%, Lucide React 80.4%, clsx 78.4%, React 100.0%, Tailwind Merge 74.5%, Radix UI 68.6%

## Checked alternatives (same job)
- TanStack Query: https://agentgid.com/tools/tanstack-react-query/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/swr/
- Homepage: https://swr.vercel.app
- Repository: https://github.com/vercel/swr
- Source: https://github.com/vercel/swr#readme
- Source: https://github.com/vercel/swr
- Source: https://github.com/vercel/swr/releases
- Source: https://swr.vercel.app/llms.txt
- Source: https://swr.vercel.app/docs/with-nextjs
- Source: https://swr.vercel.app/examples/error-handling

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
