# vercel/swr architecture

> Architecture diagram of the GitHub repository vercel/swr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vercel/swr
- Repository: https://github.com/vercel/swr

## Overview

SWR is a React Hooks data-fetching library. A component supplies a key and fetcher to `useSWR`; SWR serializes the key, manages cached request state and revalidation, and exposes updated data and errors to the component. The graph also preserves documented capabilities and the distinct infinite-loading, mutation, subscription, and server-rendering modules present in the tree. Some feature behavior is documented but its implementation was not sampled, so those connections cite the README rather than implying source inspection.

Main entry points: [`src/index/use-swr.ts`](https://github.com/vercel/swr/blob/main/src/index/use-swr.ts) (useSWR hook), [`src/_internal/utils/cache.ts`](https://github.com/vercel/swr/blob/main/src/_internal/utils/cache.ts) (Cache provider), [`src/_internal/utils/mutate.ts`](https://github.com/vercel/swr/blob/main/src/_internal/utils/mutate.ts) (Cache mutation runtime), [`src/mutation/index.ts`](https://github.com/vercel/swr/blob/main/src/mutation/index.ts) (Mutation API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["React-facing APIs"]
  node_use_swr["useSWR hook<br/>[use-swr.ts]"]
  node_public_api["Core API exports<br/>[index.ts]"]
  node_swr_config["SWRConfig provider<br/>[config-context.ts]"]
  node_server_entry["Server entry points"]
  node_immutable["Immutable hook variant<br/>[index.ts]"]
end

subgraph group_runtime["Request and cache runtime"]
  node_serialization["Key serialization<br/>[serialize.ts]"]
  node_mutate_runtime["Cache mutation runtime<br/>[mutate.ts]"]
  node_events["Revalidation events<br/>[events.ts]"]
  node_preload["Request preloading<br/>[preload.ts]"]
  node_server_preload["Server preloading<br/>[server-preload.ts]"]
end

subgraph group_extensions["Data features"]
  node_infinite["Infinite pagination<br/>[index.ts]"]
  node_subscription["Subscriptions<br/>[index.ts]"]
  node_mutation["Mutation API<br/>[index.ts]"]
  node_mutation_state["Mutation state<br/>[state.ts]"]
  node_infinite_serialization["Infinite key serialization<br/>[serialize.ts]"]
end

subgraph group_state["Shared state and config"]
  node_cache[("Cache provider<br/>[cache.ts]")]
  node_cache_helpers["Cache helpers<br/>[helper.ts]"]
  node_global_state[("Provider global state<br/>[global-state.ts]")]
  node_config["Default configuration<br/>[config.ts]"]
  node_config_merge["Config merging<br/>[merge-config.ts]"]
  node_web_preset["Web behavior preset<br/>[web-preset.ts]"]
end

node_app(("React component"))
node_fetcher(("Fetcher function"))

node_app -->|"calls hook"| node_use_swr
node_use_swr -->|"invokes fetcher"| node_fetcher
node_use_swr -->|"uses key serialization"| node_serialization
node_use_swr -->|"exported by"| node_public_api
node_use_swr -.->|"uses cache runtime"| node_cache
node_use_swr -->|"returns updates"| node_app
node_swr_config -->|"initializes provider"| node_cache
node_swr_config -->|"merges settings"| node_config_merge
node_config -->|"initializes default cache"| node_cache
node_cache -->|"binds mutations"| node_mutate_runtime
node_cache -->|"stores provider state"| node_global_state
node_cache -->|"dispatches revalidation"| node_events
node_cache -->|"uses defaults"| node_web_preset
node_mutate_runtime -->|"serializes keys"| node_serialization
node_mutate_runtime -->|"reads and writes"| node_cache_helpers
node_mutate_runtime -->|"checks mutation state"| node_global_state
node_public_api -->|"exports hook"| node_use_swr
node_public_api -->|"exports preload"| node_preload
node_preload -->|"serializes request key"| node_serialization
node_server_entry -->|"uses server preload"| node_server_preload
node_server_entry -->|"uses config exports"| node_public_api
node_infinite -->|"builds on hook"| node_use_swr
node_infinite -->|"uses key encoding"| node_infinite_serialization
node_infinite_serialization -->|"serializes page key"| node_serialization
node_mutation -->|"uses core API"| node_use_swr
node_mutation -->|"uses shared state"| node_mutation_state
node_mutation -.->|"performs cache mutations"| node_mutate_runtime
node_subscription -->|"uses core API"| node_use_swr
node_immutable -->|"wraps hook"| node_use_swr
node_use_swr -->|"revalidates on focus and reconnect"| node_events
node_use_swr -->|"supports pagination"| node_infinite
node_use_swr -->|"supports optimistic mutation"| node_mutation
node_use_swr -->|"supports SSR and SSG"| node_server_entry
node_use_swr -->|"deduplicates requests"| node_cache

click node_use_swr "https://github.com/vercel/swr/blob/main/src/index/use-swr.ts"
click node_public_api "https://github.com/vercel/swr/blob/main/src/index/index.ts"
click node_swr_config "https://github.com/vercel/swr/blob/main/src/_internal/utils/config-context.ts"
click node_infinite "https://github.com/vercel/swr/blob/main/src/infinite/index.ts"
click node_subscription "https://github.com/vercel/swr/blob/main/src/subscription/index.ts"
click node_mutation "https://github.com/vercel/swr/blob/main/src/mutation/index.ts"
click node_mutation_state "https://github.com/vercel/swr/blob/main/src/mutation/state.ts"
click node_serialization "https://github.com/vercel/swr/blob/main/src/_internal/utils/serialize.ts"
click node_infinite_serialization "https://github.com/vercel/swr/blob/main/src/infinite/serialize.ts"
click node_cache "https://github.com/vercel/swr/blob/main/src/_internal/utils/cache.ts"
click node_mutate_runtime "https://github.com/vercel/swr/blob/main/src/_internal/utils/mutate.ts"
click node_cache_helpers "https://github.com/vercel/swr/blob/main/src/_internal/utils/helper.ts"
click node_global_state "https://github.com/vercel/swr/blob/main/src/_internal/utils/global-state.ts"
click node_config "https://github.com/vercel/swr/blob/main/src/_internal/utils/config.ts"
click node_config_merge "https://github.com/vercel/swr/blob/main/src/_internal/utils/merge-config.ts"
click node_web_preset "https://github.com/vercel/swr/blob/main/src/_internal/utils/web-preset.ts"
click node_events "https://github.com/vercel/swr/blob/main/src/_internal/events.ts"
click node_preload "https://github.com/vercel/swr/blob/main/src/_internal/utils/preload.ts"
click node_server_preload "https://github.com/vercel/swr/blob/main/src/_internal/utils/server-preload.ts"
click node_server_entry "https://github.com/vercel/swr/blob/main/src/index/index.react-server.ts"
click node_immutable "https://github.com/vercel/swr/blob/main/src/immutable/index.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_use_swr,node_public_api,node_swr_config,node_server_entry,node_immutable toneBlue
class node_serialization,node_mutate_runtime,node_events,node_preload,node_server_preload toneAmber
class node_infinite,node_subscription,node_mutation,node_mutation_state,node_infinite_serialization toneMint
class node_cache,node_cache_helpers,node_global_state,node_config,node_config_merge,node_web_preset toneRose
class node_app,node_fetcher toneIndigo
```

## Components

### React-facing APIs

- [useSWR hook](https://github.com/vercel/swr/blob/main/src/index/use-swr.ts): `src/index/use-swr.ts`
- [Core API exports](https://github.com/vercel/swr/blob/main/src/index/index.ts): `src/index/index.ts`
- [SWRConfig provider](https://github.com/vercel/swr/blob/main/src/_internal/utils/config-context.ts): `src/_internal/utils/config-context.ts`
- [Server entry points](https://github.com/vercel/swr/blob/main/src/index/index.react-server.ts): `src/index/index.react-server.ts`
- [Immutable hook variant](https://github.com/vercel/swr/blob/main/src/immutable/index.ts): `src/immutable/index.ts`

### Request and cache runtime

- [Key serialization](https://github.com/vercel/swr/blob/main/src/_internal/utils/serialize.ts): `src/_internal/utils/serialize.ts`
- [Cache mutation runtime](https://github.com/vercel/swr/blob/main/src/_internal/utils/mutate.ts): `src/_internal/utils/mutate.ts`
- [Revalidation events](https://github.com/vercel/swr/blob/main/src/_internal/events.ts): `src/_internal/events.ts`
- [Request preloading](https://github.com/vercel/swr/blob/main/src/_internal/utils/preload.ts): `src/_internal/utils/preload.ts`
- [Server preloading](https://github.com/vercel/swr/blob/main/src/_internal/utils/server-preload.ts): `src/_internal/utils/server-preload.ts`

### Data features

- [Infinite pagination](https://github.com/vercel/swr/blob/main/src/infinite/index.ts): `src/infinite/index.ts`
- [Subscriptions](https://github.com/vercel/swr/blob/main/src/subscription/index.ts): `src/subscription/index.ts`
- [Mutation API](https://github.com/vercel/swr/blob/main/src/mutation/index.ts): `src/mutation/index.ts`
- [Mutation state](https://github.com/vercel/swr/blob/main/src/mutation/state.ts): `src/mutation/state.ts`
- [Infinite key serialization](https://github.com/vercel/swr/blob/main/src/infinite/serialize.ts): `src/infinite/serialize.ts`

### Shared state and config

- [Cache provider](https://github.com/vercel/swr/blob/main/src/_internal/utils/cache.ts): `src/_internal/utils/cache.ts`
- [Cache helpers](https://github.com/vercel/swr/blob/main/src/_internal/utils/helper.ts): `src/_internal/utils/helper.ts`
- [Provider global state](https://github.com/vercel/swr/blob/main/src/_internal/utils/global-state.ts): `src/_internal/utils/global-state.ts`
- [Default configuration](https://github.com/vercel/swr/blob/main/src/_internal/utils/config.ts): `src/_internal/utils/config.ts`
- [Config merging](https://github.com/vercel/swr/blob/main/src/_internal/utils/merge-config.ts): `src/_internal/utils/merge-config.ts`
- [Web behavior preset](https://github.com/vercel/swr/blob/main/src/_internal/utils/web-preset.ts): `src/_internal/utils/web-preset.ts`

### Other components

- **React component**
- **Fetcher function**

## Connections

- React component → useSWR hook: calls hook
- useSWR hook → Fetcher function: invokes fetcher
- useSWR hook → Key serialization: uses key serialization (evidence: `src/index/use-swr.ts`)
- useSWR hook → Core API exports: exported by (evidence: `src/index/index.ts`)
- useSWR hook → Cache provider: uses cache runtime
- useSWR hook → React component: returns updates
- SWRConfig provider → Cache provider: initializes provider (evidence: `src/_internal/utils/config-context.ts`)
- SWRConfig provider → Config merging: merges settings (evidence: `src/_internal/utils/config-context.ts`)
- Default configuration → Cache provider: initializes default cache (evidence: `src/_internal/utils/config.ts`)
- Cache provider → Cache mutation runtime: binds mutations (evidence: `src/_internal/utils/cache.ts`)
- Cache provider → Provider global state: stores provider state (evidence: `src/_internal/utils/cache.ts`)
- Cache provider → Revalidation events: dispatches revalidation (evidence: `src/_internal/utils/cache.ts`)
- Cache provider → Web behavior preset: uses defaults (evidence: `src/_internal/utils/cache.ts`)
- Cache mutation runtime → Key serialization: serializes keys (evidence: `src/_internal/utils/mutate.ts`)
- Cache mutation runtime → Cache helpers: reads and writes (evidence: `src/_internal/utils/mutate.ts`)
- Cache mutation runtime → Provider global state: checks mutation state (evidence: `src/_internal/utils/mutate.ts`)
- Core API exports → useSWR hook: exports hook (evidence: `src/index/index.ts`)
- Core API exports → Request preloading: exports preload (evidence: `src/index/index.ts`)
- Request preloading → Key serialization: serializes request key (evidence: `src/_internal/utils/preload.ts`)
- Server entry points → Server preloading: uses server preload (evidence: `src/index/index.react-server.ts`)
- Server entry points → Core API exports: uses config exports (evidence: `src/index/index.react-server.ts`)
- Infinite pagination → useSWR hook: builds on hook (evidence: `src/infinite/index.ts`)
- Infinite pagination → Infinite key serialization: uses key encoding (evidence: `src/infinite/index.ts`)
- Infinite key serialization → Key serialization: serializes page key (evidence: `src/infinite/serialize.ts`)
- Mutation API → useSWR hook: uses core API (evidence: `src/mutation/index.ts`)
- Mutation API → Mutation state: uses shared state (evidence: `src/mutation/index.ts`)
- Mutation API → Cache mutation runtime: performs cache mutations
- Subscriptions → useSWR hook: uses core API (evidence: `src/subscription/index.ts`)
- Immutable hook variant → useSWR hook: wraps hook (evidence: `src/immutable/index.ts`)
- useSWR hook → Revalidation events: revalidates on focus and reconnect
- useSWR hook → Infinite pagination: supports pagination
- useSWR hook → Mutation API: supports optimistic mutation
- useSWR hook → Server entry points: supports SSR and SSG
- useSWR hook → Cache provider: deduplicates requests
