vercel/swr architecture
SWR is a modular monolith TypeScript library for React that provides efficient, flexible data fetching and caching. It features a layered architecture with distinct modules for core engine, APIs, utilities, types, configuration, testing, and examples. The core engine manages caching, state, and middleware, while API modules expose hooks for various data fetching scenarios including infinite loading, mutation, immutable fetching, and subscriptions. React Context and observer/subscriber patterns enable scalable configuration and cache updates. The system balances modularity and monolithic deployment, ensuring maintainability and simplicity for consumers. Extensive testing and examples support reliability and developer experience.
Architecture: modular monolith. Built with TypeScript, JavaScript, React, Next.js, Jest, Playwright, pnpm, Immer (in examples), Lodash.debounce (in examples), @reach/combobox (in examples).
- core-api: Exposes the main SWR hook, configuration, and utility functions for consumers. This is the primary entry point for users of the library.
- infinite-api: Provides the infinite loading variant of SWR, allowing consumers to fetch paginated data efficiently.
- immutable-api: Implements the immutable data fetching variant, ensuring data is not revalidated or mutated after initial fetch.
- mutation-api: Implements the mutation API, allowing consumers to trigger and manage remote data mutations.
- subscription-api: Implements the subscription API, enabling real-time data updates via subscriptions.
- core-engine: Contains the core engine for SWR, including configuration context, event handling, cache initialization, and core business logic for data fetching and state management.
- middleware: Implements middleware composition and built-in middleware presets for extending SWR's behavior.
- cache: Implements the caching layer, including cache initialization, mutation, subscription, and tag management.
- state: Handles state management, including global state, state dependencies, and state transitions for SWR hooks.
- utils: Provides utility functions for serialization, hashing, environment detection, and other helpers used throughout the codebase.
- types: Defines all TypeScript types, interfaces, and configuration schemas used throughout SWR.
- config: Contains configuration files for TypeScript, Jest, and project-wide settings.
- testing: Contains all test suites, including unit, type, and end-to-end tests for SWR.
- examples: Contains example applications demonstrating SWR usage in various scenarios, including Next.js, React, and advanced patterns.