Project Overview
Project Purpose and Objectives
This project implements a robust, flexible, and efficient data fetching and state management library centered around the SWR (stale-while-revalidate) strategy for React applications. It aims to provide a comprehensive set of hooks and utilities to simplify asynchronous data fetching, caching, revalidation, and mutation with built-in support for concurrency and suspense features from React.
The primary purpose is to deliver a developer-friendly, extensible SWR implementation with advanced capabilities such as infinite loading, subscription-based updates, manual retries, and mutation handling, while maintaining optimal performance and developer ergonomics.
Major Functionalities and Their Implementation
Core Data Fetching (
useSWR):
Provides a React hook for fetching data with caching, revalidation, and suspense support. It manages internal state, global cache, and implements various revalidation triggers (focus, reconnect, interval).Infinite Loading (useSWRInfinite):
Extends the core hook to support paginated and infinite data sources with page size management and data concatenation.Remote Data Mutation (useSWRMutation):
Enables controlled remote mutations (POST, PUT, DELETE, PATCH) with optimistic updates, rollback on error, and cache synchronization.Subscriptions (useSWRSubscription):
Adds middleware and hooks to subscribe to external data sources for real-time updates, managing lifecycle and cleanup to avoid leaks.Preloading Data:
Utilities and middleware to preload data before it is used by components, reducing loading waterfalls and improving perceived performance.Middleware Architecture:
Implements middleware composition for features like devtools integration, preloading, and immutable data handling, allowing flexible extension.Error Handling and Retry Logic:
Supports suspense error boundaries, manual retry mechanisms, and configurable retry strategies with exponential backoff.Server-Side Rendering (SSR) & Streaming:
Supports SSR with hydration, concurrent React features, and partial hydration with Suspense, enabling performant server-client transitions.Developer Tooling and Debugging:
Includes devtools integration and debug history hooks to trace state changes, enhancing developer experience.Examples and E2E Tests:
The project includes comprehensive example applications demonstrating various use cases (optimistic UI, infinite scroll, subscriptions, focus revalidation) and end-to-end tests verifying rendering, concurrency, mutation, and error scenarios.
The system is primarily implemented in React with TypeScript, leveraging modern React features such as hooks, Suspense, and concurrent rendering capabilities.
Example Workflows and Use Cases
1. Manual Retry with Suspense and Error Boundary (Remote Data Fetching)
Scenario: Fetch remote data that may fail initially, requiring manual retry.
Workflow:
Component uses
useRemoteDatahook to fetch data with SWR and suspense.Wrapped with
ErrorBoundaryto catch fetch errors.On error, fallback UI displays a retry button.
Retry triggers SWR's
mutateto revalidate and resets the error state.
Files:
e2e/site/component/manual-retry.tsx
2. Infinite Loading for Paginated Data
Scenario: Fetch GitHub issues for a repo with pagination.
Workflow:
Component manages repository input and passes to useSWRInfinite.
useSWRInfinite calls fetcher for each page, manages caching and concatenation.
Load more button increments page size to fetch next batch.
Refresh button triggers revalidation for all loaded pages.
Files:
examples/infinite/pages/index.js
3. Remote Mutation with Status Feedback
Scenario: Trigger a server-side action (mutation) and show status.
Workflow:
Component uses useSWRMutation hook bound to API endpoint.
Button triggers mutation; UI shows loading, success, or error states.
Mutation can optimistically update cache or rollback on failure.
Files:
4. Subscription to External Event Source
Scenario: Subscribe to an event emitter emitting data/errors periodically.
Workflow:
Component uses useSWRSubscription middleware to listen to events.
Updates component state on new data or errors.
Cleans up subscription on unmount.
Files:
5. Suspense with Preloading to Avoid Waterfalls
Scenario: Preload data before rendering to avoid loading cascading delays.
Workflow:
Preload function called to fetch data and cache results ahead.
Component uses suspense-enabled SWR hook to consume cached data.
Suspense fallback shown only if preload not complete.
Files:
Stack and Technologies
Languages:
TypeScript (for core library and typings)
JavaScript (for examples and tests)
React (v18+ for hooks, Suspense, concurrent features)
Frameworks and Libraries:
React: Primary UI library for components and hooks.
Next.js: Used in examples and e2e site for SSR, routing, and API routes.
SWR: Core data fetching library implemented with hooks and middleware.
SWR Middleware Architecture: To extend and enhance functionality modularly.
React Error Boundary: For error handling in suspense components.
Playwright: For end-to-end testing.
Jest + React Testing Library: For unit and integration tests.
Immer: Used in optimistic UI example for immutable updates.
Axios: Used in axios examples for HTTP requests.
Debounce (lodash.debounce): Used in autocomplete example for input optimization.
EventEmitter: Node.js events for subscription example.
Build Tools and Config:
Next.js configurations for experimental features (e.g., server actions).
pnpm workspace to manage packages and examples.
Jest and Playwright configurations for testing.
Why These Technologies:
React and Next.js provide a modern and versatile platform supporting SSR, CSR, and concurrent UI paradigms.
SWR is the core concept implemented here, chosen for its declarative, hook-based data fetching approach.
Middleware and subscription patterns promote extensibility and real-time capabilities.
Testing frameworks ensure reliability across complex async and suspense scenarios.
Examples demonstrate broad applicability and practical use cases for users.
High-Level Architecture
The project is structured primarily as a React hook-based library with multiple layers and modules:
Core SWR Library (
src/index,src/_internal):Implements the core
useSWRhook with caching, revalidation, suspense, error handling, and middleware support.Internal utilities manage cache, serialization, mutation, event handling, and global state.
Middleware modules add features like immutable fetching, preloading, devtools integration, and subscriptions.
Extended Hooks and Features:
useSWRInfinite for infinite scroll pagination.
useSWRMutation for mutation handling.
useSWRSubscription for external subscription management.
Examples and E2E Site (
e2e/site,examples/):Show practical use cases and integration patterns.
Include API routes, components demonstrating suspense, retry, mutation, infinite loading, subscriptions, etc.
E2E tests validate behaviors for rendering, retries, suspense, mutation, concurrency, and error boundaries.
Testing Suite (
test/):
Extensive unit, integration, and E2E tests using Jest, React Testing Library, and Playwright
Component Interaction
Frontend React Components use SWR hooks for data fetching, mutation, and subscription, wrapped in Suspense and ErrorBoundary for loading and error states.
API Routes serve JSON data or simulate delayed/failing endpoints to test SWR behaviors.
Middleware layers compose additional functionality transparently into hooks.
Cache Providers manage shared state and deduplication across hooks and components.
Subscription Middleware manages real-time data sources and cleanup.
Mermaid Component Diagram
graph TB
subgraph React Frontend
FE[React Components] -->|useSWR hooks| SWR[useSWR Core Hook]
SWR --> Middleware[Middleware Layer]
Middleware --> Cache[Cache Provider]
end
subgraph Server/API
API[Next.js API Routes]
end
Cache -->|fetch requests| API
FE -->|mutations/events| API
SWR --> Subscription[Subscription Middleware]
Subscription --> Cache
Developer Navigation
Frontend Developers
Explore
e2e/site/appande2e/site/componentfor practical React components demonstrating usage of the SWR hooks with Suspense, ErrorBoundary, retries, and mutations.Refer to example apps in
examples/for real-world implementations:infinite-scrollfor pagination patterns.optimistic-uiandoptimistic-ui-immerfor mutation and optimistic updates.subscriptionfor real-time event-driven data fetching.suspense-retryfor advanced suspense and retry logic.
Key files:
e2e/site/component/manual-retry.tsxanduse-remote-data.tsfor retry and suspense.examples/infinite/pages/index.jsandlibs/fetch.jsfor infinite loading.
Backend/API Developers
Inspect API route handlers under
e2e/site/pages/api/andexamples/**/pages/api/for various data endpoints, including simulated delay and error behaviors.Mutation server actions under
e2e/site/app/mutate-server-actionshowcase server functions invoked by mutations.
Core Library Contributors
Core SWR implementation in
src/index/use-swr.tsand internal utilities insrc/_internal/utils/.Middleware implementations in
src/_internal/utils/and feature modules insrc/mutation/,src/infinite/, andsrc/subscription/.Type definitions and middleware contracts in
src/_internal/types.ts,src/mutation/types.ts,src/infinite/types.ts, andsrc/subscription/types.ts.Testing and validation in
test/directory with extensive unit and integration tests.
Testing and Quality Assurance
End-to-end test suites in
e2e/test/covering concurrency, rendering, mutation, suspense, and subscription scenarios.Unit and integration tests in
test/for hooks, middleware, and utilities.Use
playwright.config.jsfor E2E test configuration.
Visual Diagrams
1. High-Level Component Diagram (See above)
Illustrates the flow between React components, SWR hooks, middleware, cache providers, and API routes.
2. Key Workflow Flowchart: Manual Retry with Suspense and ErrorBoundary
flowchart TD
A[Component Mounts] --> B[useRemoteData Hook Fetches Data]
B -->|First Fetch Fails| C[ErrorBoundary Catches Error]
C --> D[Fallback UI with Retry Button]
D -->|Retry Clicked| E[Trigger SWR mutate to Revalidate]
E --> B
B -->|Fetch Succeeds| F[Display Data]
This flow demonstrates how manual retry is coordinated with SWR's cache mutation and React suspense/error boundary to manage loading, error, and retry states.
This "Project Overview" provides a concise yet thorough roadmap highlighting the project's goals, key features, architecture, and developer entry points to facilitate efficient onboarding and contribution.