# vercel/swr

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/vercel-swr).**

32,406 stars · 1,351 forks · TypeScript · MIT

## Links

- GitHub: https://github.com/vercel/swr
- Homepage: https://swr.vercel.app
- awesome-repositories: https://awesome-repositories.com/repository/vercel-swr.md

## Topics

`cache` `data` `data-fetching` `fetch` `hook` `hooks` `nextjs` `react` `react-native` `stale-while-revalidate` `suspense` `swr` `vercel`

## Description

SWR is a data fetching library that provides a collection of hooks for managing remote data synchronization, caching, and state updates in web applications. It employs a declarative approach to handle complex network request lifecycles and dependency chains, ensuring that client-side application state remains consistent with server data through automatic revalidation and background updates.

The library distinguishes itself through a reactive cache layer that automatically synchronizes local state with remote sources based on component lifecycle events. It features event-driven revalidation, which triggers background refreshes in response to browser-level changes like window focus or network reconnection. To enhance user experience, it supports optimistic cache mutation, allowing the interface to update immediately while performing background network requests, with built-in rollback capabilities if a mutation fails.

Beyond core fetching, the library offers a comprehensive suite of tools for managing paginated data streams, real-time subscriptions, and request retry logic. It includes robust support for server-side integration, enabling data pre-rendering and hydration to ensure fast initial page loads. The architecture is highly extensible, allowing developers to intercept and modify the request lifecycle through middleware composition and custom cache providers.

The library is built with TypeScript, providing full type safety for hooks, configuration objects, and middleware definitions. It is designed to be installed as a dependency in modern web projects, offering a centralized configuration context that propagates settings and cache instances throughout the component tree.

## Tags

### Web Development

- [Data Fetching Libraries](https://awesome-repositories.com/f/web-development/data-fetching-libraries.md) — Provides declarative hooks to manage network request lifecycles and simplify state handling.
- [Data Fetching State Management](https://awesome-repositories.com/f/web-development/data-fetching-state-management.md) — Propagates global configuration and cache instances through the component tree for centralized data management.
- [Server-Side Data Prefetching](https://awesome-repositories.com/f/web-development/server-side-data-prefetching.md) — Loads data on the server before sending pages to the browser to ensure users see complete content immediately. ([source](https://swr.vercel.app/docs/with-nextjs))
- [State Synchronization Tools](https://awesome-repositories.com/f/web-development/state-synchronization-tools.md) — Keeps client-side application state in sync with server data through automatic revalidation and caching.
- [Optimistic UI Updates](https://awesome-repositories.com/f/web-development/optimistic-ui-updates.md) — Updates the local data store immediately to provide instant UI feedback with automatic rollback on failure.
- [Server-Side Suspense Configurations](https://awesome-repositories.com/f/web-development/server-side-suspense-configurations.md) — Provides initial data via fallback options during server-side rendering to ensure components have necessary information. ([source](https://swr.vercel.app/docs/suspense))
- [Suspense Integrations](https://awesome-repositories.com/f/web-development/suspense-integrations.md) — Guarantees that fetched data is available immediately upon component render to simplify data handling. ([source](https://swr.vercel.app/docs/suspense))
- [Data Synchronization Strategies](https://awesome-repositories.com/f/web-development/data-synchronization-strategies.md) — Triggers background data refreshes based on browser events to maintain synchronization with server state.
- [Infinite Data Fetching](https://awesome-repositories.com/f/web-development/infinite-data-fetching.md) — Retrieves multiple pages of data sequentially or in parallel to support infinite-scroll interfaces. ([source](https://swr.vercel.app/docs/pagination))
- [Optimistic UI Patterns](https://awesome-repositories.com/f/web-development/optimistic-ui-patterns.md) — Updates the user interface immediately after actions to ensure a responsive and fluid experience.
- [Suspense Integration](https://awesome-repositories.com/f/web-development/suspense-integration.md) — Coordinates with the rendering engine to pause component execution until data is available.
- [Cache Pre-filling](https://awesome-repositories.com/f/web-development/cache-pre-filling.md) — Injects initial data into the cache to provide immediate values to hooks before the first network request completes. ([source](https://swr.vercel.app/docs/prefetching))
- [Custom Fetchers](https://awesome-repositories.com/f/web-development/custom-fetchers.md) — Supports creating custom data retrieval functions using standard HTTP clients. ([source](https://swr.vercel.app/docs/data-fetching))
- [Dependent Data Fetching](https://awesome-repositories.com/f/web-development/dependent-data-fetching.md) — Requests data that relies on previous results by using a function as the key. ([source](https://swr.vercel.app/docs/conditional-fetching))
- [Global Configuration](https://awesome-repositories.com/f/web-development/global-configuration.md) — Enables defining data fetching options globally or within nested contexts. ([source](https://swr.vercel.app/docs/global-configuration))
- [Programmatic Prefetching](https://awesome-repositories.com/f/web-development/programmatic-prefetching.md) — Triggers data fetching using a dedicated API to populate the cache before components render. ([source](https://swr.vercel.app/docs/prefetching))
- [Request Deduplication](https://awesome-repositories.com/f/web-development/request-deduplication.md) — Collapses multiple concurrent requests for the same resource into a single network call using unique keys.
- [Server-Side Hydration Utilities](https://awesome-repositories.com/f/web-development/server-side-hydration-utilities.md) — Generates server-rendered pages with initial data so client-side components can hydrate immediately. ([source](https://swr.vercel.app/docs/with-nextjs))
- [Cache Providers](https://awesome-repositories.com/f/web-development/cache-providers.md) — Allows specifying a custom storage mechanism for cached data via provider functions. ([source](https://swr.vercel.app/docs/global-configuration))
- [Conditional Data Fetching](https://awesome-repositories.com/f/web-development/conditional-data-fetching.md) — Prevents requests from starting by passing null or falsy values as the key. ([source](https://swr.vercel.app/docs/conditional-fetching))
- [Dependency Tracking](https://awesome-repositories.com/f/web-development/dependency-tracking.md) — Maps complex objects to stable identifiers to detect data changes and trigger re-fetches.
- [Resource Preloading](https://awesome-repositories.com/f/web-development/resource-preloading.md) — Initiates network requests using native browser link tags to populate the cache before JavaScript execution begins. ([source](https://swr.vercel.app/docs/prefetching))
- [Data Fetching Configuration](https://awesome-repositories.com/f/web-development/data-fetching-configuration.md) — Configures how data loads in server components by using serialization utilities and providers. ([source](https://swr.vercel.app/docs/with-nextjs))
- [Focus-Based Revalidation](https://awesome-repositories.com/f/web-development/focus-based-revalidation.md) — Refreshes data automatically when the user returns to the browser tab. ([source](https://swr.vercel.app/docs/revalidation))
- [Global State Access](https://awesome-repositories.com/f/web-development/global-state-access.md) — Provides a dedicated hook to retrieve the current global configuration and cache instance. ([source](https://swr.vercel.app/docs/global-configuration))
- [Middleware Hook Composition](https://awesome-repositories.com/f/web-development/middleware-hook-composition.md) — Wraps data fetching lifecycles in function chains to intercept and modify requests.
- [Pagination Utilities](https://awesome-repositories.com/f/web-development/pagination-utilities.md) — Manages paginated data streams to build seamless content feeds that grow as users navigate.
- [Polling Strategies](https://awesome-repositories.com/f/web-development/polling-strategies.md) — Fetches fresh data at set time intervals while the component remains visible. ([source](https://swr.vercel.app/docs/revalidation))
- [Reconnect-Based Revalidation](https://awesome-repositories.com/f/web-development/reconnect-based-revalidation.md) — Triggers an automatic data refresh when the device regains network connectivity. ([source](https://swr.vercel.app/docs/revalidation))

### Data & Databases

- [Reactive Cache Layers](https://awesome-repositories.com/f/data-databases/reactive-cache-layers.md) — Provides a persistent storage mechanism that automatically synchronizes local state with remote server data.
- [Optimistic Update Management](https://awesome-repositories.com/f/data-databases/optimistic-update-management.md) — Updates the local cache immediately before a remote mutation finishes with automatic rollback capabilities. ([source](https://swr.vercel.app/docs/mutation))
- [Cache Mutation Utilities](https://awesome-repositories.com/f/data-databases/cache-mutation-utilities.md) — Updates local cache data and triggers revalidation for specific keys using bound mutator functions. ([source](https://swr.vercel.app/docs/mutation))
- [Remote Mutation Execution](https://awesome-repositories.com/f/data-databases/remote-mutation-execution.md) — Executes manual remote mutations using dedicated hooks that manage their own state. ([source](https://swr.vercel.app/docs/mutation))

### Networking & Communication

- [Real-time Data Subscriptions](https://awesome-repositories.com/f/networking-communication/real-time-data-subscriptions.md) — Connects to real-time data sources to update component state automatically when new events arrive. ([source](https://swr.vercel.app/docs/subscription))

### Part of an Awesome List

- [API Clients and Utilities](https://awesome-repositories.com/f/awesome-lists/data/api-clients-and-utilities.md) — React hooks for remote data fetching.
- [State Management](https://awesome-repositories.com/f/awesome-lists/data/state-management.md) — Hooks for data fetching and caching.
- [State Management](https://awesome-repositories.com/f/awesome-lists/devtools/state-management.md) — React hooks for efficient data fetching.

### Programming Languages & Runtimes

- [Type System Tools](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/type-system-tools.md) — Infers data, error, and key types automatically from fetcher functions to ensure type safety. ([source](https://swr.vercel.app/docs/typescript))

### Testing & Quality Assurance

- [API Testing](https://awesome-repositories.com/f/testing-quality-assurance/api-network-testing/api-testing.md) — Provides configurable automatic request retries using exponential backoff. ([source](https://swr.vercel.app/docs/error-handling))
