How vercel/swr works
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: use-swr.ts useSWR hook · cache.ts Cache provider · mutate.ts Cache mutation runtime · index.ts Mutation API
Components 23
React-facing APIs
- useSWR hook
src/index/use-swr.ts - Core API exports
src/index/index.ts - SWRConfig provider
src/_internal/utils/config-context.ts - Server entry points
src/index/index.react-server.ts - Immutable hook variant
src/immutable/index.ts
Request and cache runtime
- Key serialization
src/_internal/utils/serialize.ts - Cache mutation runtime
src/_internal/utils/mutate.ts - Revalidation events
src/_internal/events.ts - Request preloading
src/_internal/utils/preload.ts - Server preloading
src/_internal/utils/server-preload.ts
Data features
- Infinite pagination
src/infinite/index.ts - Subscriptions
src/subscription/index.ts - Mutation API
src/mutation/index.ts - Mutation state
src/mutation/state.ts - Infinite key serialization
src/infinite/serialize.ts
Shared state and config
- Cache provider
src/_internal/utils/cache.ts - Cache helpers
src/_internal/utils/helper.ts - Provider global state
src/_internal/utils/global-state.ts - Default configuration
src/_internal/utils/config.ts - Config merging
src/_internal/utils/merge-config.ts - Web behavior preset
src/_internal/utils/web-preset.ts
Other components
- React component
- Fetcher function
Connections 34
- React component → useSWR hook: calls hook
- useSWR hook → Fetcher function: invokes fetcher
- useSWR hook → Key serialization: uses key serialization
src/index/use-swr.ts - useSWR hook → Core API exports: exported by
src/index/index.ts - useSWR hook → Cache provider: uses cache runtime
- useSWR hook → React component: returns updates
- SWRConfig provider → Cache provider: initializes provider
src/_internal/utils/config-context.ts - SWRConfig provider → Config merging: merges settings
src/_internal/utils/config-context.ts - Default configuration → Cache provider: initializes default cache
src/_internal/utils/config.ts - Cache provider → Cache mutation runtime: binds mutations
src/_internal/utils/cache.ts - Cache provider → Provider global state: stores provider state
src/_internal/utils/cache.ts - Cache provider → Revalidation events: dispatches revalidation
src/_internal/utils/cache.ts - Cache provider → Web behavior preset: uses defaults
src/_internal/utils/cache.ts - Cache mutation runtime → Key serialization: serializes keys
src/_internal/utils/mutate.ts - Cache mutation runtime → Cache helpers: reads and writes
src/_internal/utils/mutate.ts - Cache mutation runtime → Provider global state: checks mutation state
src/_internal/utils/mutate.ts - Core API exports → useSWR hook: exports hook
src/index/index.ts - Core API exports → Request preloading: exports preload
src/index/index.ts - Request preloading → Key serialization: serializes request key
src/_internal/utils/preload.ts - Server entry points → Server preloading: uses server preload
src/index/index.react-server.ts - Server entry points → Core API exports: uses config exports
src/index/index.react-server.ts - Infinite pagination → useSWR hook: builds on hook
src/infinite/index.ts - Infinite pagination → Infinite key serialization: uses key encoding
src/infinite/index.ts - Infinite key serialization → Key serialization: serializes page key
src/infinite/serialize.ts - Mutation API → useSWR hook: uses core API
src/mutation/index.ts - Mutation API → Mutation state: uses shared state
src/mutation/index.ts - Mutation API → Cache mutation runtime: performs cache mutations
- Subscriptions → useSWR hook: uses core API
src/subscription/index.ts - Immutable hook variant → useSWR hook: wraps hook
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