{
 "kind": "tool",
 "slug": "swr",
 "path": "tools/swr/",
 "name": "SWR",
 "maker": null,
 "density": "profile",
 "crumbs": [
  [
   "Tools",
   "tools/"
  ],
  [
   "State and data fetching",
   "tools/state-data/"
  ]
 ],
 "lead": "React hooks library for data fetching with caching, revalidation and real-time updates.",
 "chips": [
  {
   "label": "State and data fetching",
   "path": "tools/state-data/"
  },
  {
   "label": "NPM"
  }
 ],
 "primary": {
  "label": "Copy install command",
  "copy": "npm install swr"
 },
 "links": [
  {
   "label": "GitHub",
   "href": "https://github.com/vercel/swr"
  },
  {
   "label": "Website",
   "href": "https://swr.vercel.app"
  }
 ],
 "track": "New versions, deprecation and license changes on your dashboard",
 "verdicts": [
  {
   "key": "license",
   "label": "License",
   "value": "MIT",
   "status": "yes",
   "sub": "Use it in paid apps",
   "quote": null,
   "note": "License from npm.",
   "src": "https://github.com/vercel/swr"
  },
  {
   "key": "maint",
   "label": "Maintained",
   "value": "Maintained",
   "status": "yes",
   "sub": "Last change Oct 16, 2026",
   "note": "Latest GitHub release Aug 12, 2026. ",
   "src": "https://github.com/vercel/swr"
  },
  {
   "key": "pop",
   "label": "Used by agents' projects",
   "value": "51 projects",
   "status": "info",
   "sub": "#5 of 53 in its category",
   "note": "Dependency in 51 public GitHub projects set up for AI coding agents that we scanned (0.8%).",
   "src": null
  },
  {
   "key": "agent",
   "label": "Agent access",
   "value": "Docs for agents",
   "status": "yes",
   "sub": "llms.txt",
   "note": "Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.",
   "src": "https://swr.vercel.app/llms.txt"
  }
 ],
 "vfoot": "From the package registry and GitHub, checked daily.",
 "blocks": [
  {
   "id": "overview",
   "nav": "Overview",
   "h2": "What SWR does",
   "parts": [
    {
     "t": "prose",
     "text": "SWR puts the stale-while-revalidate cache rule behind a single React hook: it returns cached data at once, refetches in the background, then re-renders, and newer versions can seed that cache from React Server Components."
    }
   ]
  },
  {
   "id": "popularity",
   "nav": "Popularity",
   "h2": "SWR popularity",
   "parts": [
    {
     "t": "stats",
     "items": [
      {
       "n": "77",
       "of": "/ 100",
       "l": "Gid Score, #148 overall"
      },
      {
       "n": "28.5M",
       "l": "downloads a week, npm"
      },
      {
       "n": "32.5K",
       "l": "GitHub stars"
      },
      {
       "n": "51",
       "l": "agent projects use it"
      }
     ]
    },
    {
     "t": "small",
     "text": "Gid Score measures public popularity and activity, not quality.",
     "link": "How the Gid Score is calculated",
     "path": "tools/top/"
    }
   ]
  },
  {
   "id": "usage",
   "nav": "How it is used",
   "h2": "How SWR is used",
   "parts": [
    {
     "t": "list",
     "items": [
      "SWR is a dependency in 51 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (1.0% of the 4,867 JavaScript/TypeScript ones), #5 of 53 in state and data fetching.",
      "Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 239; those are left out of every share on this page.",
      "The most used alternative in our scan is TanStack Query (458 projects).",
      "Projects that use it most often also use Next.js (88.2%), Lucide React (80.4%), clsx (78.4%)."
     ]
    },
    {
     "t": "h3",
     "text": "By coding agent"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "32 projects, 1.3%",
       "v": "Cursor",
       "path": "agents/cursor/"
      },
      {
       "l": "26 projects, 0.8%",
       "v": "Claude Code",
       "path": "agents/claude-code/"
      },
      {
       "l": "18 projects, 0.6%",
       "v": "GitHub Copilot",
       "path": "agents/github-copilot/"
      }
     ]
    },
    {
     "t": "h3",
     "text": "Often used with"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "In 88.2% of its projects",
       "v": "Next.js",
       "path": "tools/next/"
      },
      {
       "l": "In 80.4% of its projects",
       "v": "Lucide React",
       "path": "tools/lucide-react/"
      },
      {
       "l": "In 78.4% of its projects",
       "v": "clsx",
       "path": "tools/clsx/"
      },
      {
       "l": "In 100.0% of its projects",
       "v": "React",
       "path": "tools/react/"
      },
      {
       "l": "In 74.5% of its projects",
       "v": "Tailwind Merge",
       "path": "tools/tailwind-merge/"
      },
      {
       "l": "In 68.6% of its projects",
       "v": "Radix UI",
       "path": "tools/radix-ui/"
      },
      {
       "l": "In 68.6% of its projects",
       "v": "Class Variance Authority",
       "path": "tools/class-variance-authority/"
      },
      {
       "l": "In 90.2% of its projects",
       "v": "Tailwind CSS",
       "path": "tools/tailwindcss/"
      }
     ]
    }
   ]
  },
  {
   "id": "profile",
   "nav": "Profile",
   "h2": "SWR in practice",
   "parts": [
    {
     "t": "h3",
     "text": "Strengths"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "Bring your own fetcher",
       "v": "SWR does not ship an HTTP client. The README says the fetcher can be any async function. Startup House wrapped its existing axios instance, with interceptors and headers, and used that as the fetcher."
      },
      {
       "l": "Repeat calls to the same key are collapsed",
       "v": "Requests that share a key inside <code>dedupingInterval</code> (2000 ms by default) are deduplicated. A deeply nested child can call the same hook as its parent without firing a second request."
      },
      {
       "l": "Refetches on focus and reconnect without setup",
       "v": "<code>revalidateOnFocus</code> and <code>revalidateOnReconnect</code> both default to <code>true</code>. Polling is opt-in through <code>refreshInterval</code>, which can be a number or a function of the latest data."
      },
      {
       "l": "Server prefetch that skips the duplicate client request",
       "v": "In the App Router, <code>preload</code> in a Server Component plus <code>SWRConfig</code>'s <code>cacheData</code> hands server-fetched data to client hooks. The docs say the first client request is then skipped, and later revalidations use the client fetcher."
      }
     ]
    },
    {
     "t": "h3",
     "text": "Watch out for"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "Hooks are client-only",
       "v": "<code>useSWR</code>, <code>useSWRInfinite</code> and the mutation hook cannot be imported in React Server Components. Only <code>SWRConfig</code>, <code>preload</code> and the key serialization APIs work there."
      },
      {
       "l": "Server prefetch is experimental",
       "v": "The <code>preload</code> + <code>cacheData</code> pattern needs a recent version and is flagged as experimental in the docs."
      },
      {
       "l": "cacheData goes on SWRConfig only",
       "v": "You cannot pass <code>cacheData</code> to an individual hook. Array or function keys also have to be serialized with <code>unstable_serialize</code> when you build <code>cacheData</code> by hand."
      }
     ]
    },
    {
     "t": "h3",
     "text": "Quick start: fetch a user"
    },
    {
     "t": "tabs",
     "label": "Example",
     "items": [
      {
       "tab": "Jsx",
       "code": "import useSWR from 'swr'\n\nfunction Profile() {\n  const { data, error, isLoading } = useSWR('/api/user', fetcher)\n\n  if (error) return <div>failed to load</div>\n  if (isLoading) return <div>loading...</div>\n  return <div>hello {data.name}!</div>\n}"
      }
     ]
    }
   ]
  },
  {
   "id": "more",
   "nav": null,
   "spy_as": "profile",
   "h2": "More about SWR",
   "parts": [
    {
     "t": "more",
     "items": [
      {
       "title": "Head to head",
       "items": [
        {
         "text": "TanStack Query vs SWR",
         "note": "used together in 14 projects",
         "path": "tools/compare/tanstack-query-vs-swr/"
        }
       ]
      },
      {
       "title": "Packages around it",
       "items": [
        "next: React framework",
        "clsx: className builder",
        "lucide-react: Icon set",
        "react: UI library",
        "tailwind-merge: Tailwind class merging",
        "@radix-ui: UI primitives"
       ]
      },
      {
       "title": "More in State and data fetching",
       "path": "tools/state-data/",
       "items": [
        {
         "text": "Zustand",
         "path": "tools/zustand/"
        },
        {
         "text": "RxJS",
         "path": "tools/rxjs/"
        },
        {
         "text": "Pinia",
         "path": "tools/pinia/"
        },
        {
         "text": "nuqs",
         "path": "tools/nuqs/"
        },
        {
         "text": "Immer",
         "path": "tools/immer/"
        },
        {
         "text": "React Redux",
         "path": "tools/react-redux/"
        },
        {
         "text": "Redux Toolkit",
         "path": "tools/reduxjs/"
        },
        {
         "text": "Jotai",
         "path": "tools/jotai/"
        }
       ]
      }
     ]
    }
   ]
  },
  {
   "id": "alternatives",
   "nav": "Alternatives",
   "h2": "Alternatives",
   "parts": [
    {
     "t": "alts",
     "items": [
      {
       "name": "SWR",
       "me": true,
       "facts": [
        "MIT",
        "Gid 77",
        "28.5M/wk"
       ]
      },
      {
       "name": "TanStack Query",
       "why": "Hooks for managing, caching and synchronizing remote data in React.",
       "path": "tools/tanstack-react-query/",
       "facts": [
        "MIT",
        "Gid 88",
        "92M/wk"
       ]
      }
     ],
     "more": "Compare SWR with another tool",
     "more_path": "tools/compare/"
    }
   ]
  },
  {
   "id": "start",
   "nav": "Get started",
   "h2": "Get started",
   "parts": [
    {
     "t": "install",
     "intro": "Install it.",
     "code": "npm install swr"
    },
    {
     "t": "h3",
     "text": "Rules for your agent"
    },
    {
     "t": "rules",
     "label": "Paste into AGENTS.md or CLAUDE.md",
     "text": "## SWR\n- Call `useSWR`, `useSWRInfinite` and `useSWRMutation` only in Client Components (`'use client'`). In Server Components, import only `SWRConfig`, `preload` and `unstable_serialize`.\n- 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.\n- To prefetch on the server, call `preload(key, fetcher)` and pass the merged results to `<SWRConfig value={{ cacheData }}>`. Never pass `cacheData` to `useSWR`. This requires SWR 2.5.0-beta.1+ and is experimental.\n- When building `cacheData` by hand with array or function keys, serialize them with `unstable_serialize`.\n- Show spinners from `isLoading`, which is only true with no loaded data. Use `isValidating` for background refreshes, such as disabling a Retry button.\n- With `useSWRInfinite`, return `null` from `getKey` when the previous page is empty, to stop fetching.\n- Retries are on by default (`shouldRetryOnError = true`). Set it to `false` if the UI offers its own retry via `mutate()`.\n"
    }
   ]
  }
 ],
 "glance": [
  [
   "Package",
   "npm swr"
  ],
  [
   "Category",
   "State and data fetching"
  ],
  [
   "License",
   "MIT"
  ],
  [
   "Latest version",
   "v2.5.1, August 12, 2026"
  ],
  [
   "On GitHub since",
   "October 2019"
  ],
  [
   "Gid Score",
   "77.3"
  ],
  [
   "Rank in category",
   "#5 of 53"
  ],
  [
   "GitHub",
   "vercel/swr"
  ],
  [
   "Last change",
   "Oct 16, 2026"
  ]
 ],
 "good": [],
 "checked_line": "From the package registry, GitHub and our scan of public agent projects; numbers update daily.",
 "logo": "https://agentgid.com/media/avatars/vercel.png"
}