Built-in Middlewares

Purpose

This subtopic focuses on the aggregation and composition of essential middleware layers bundled with the SWR library. These built-in middlewares provide foundational, commonly used enhancements—such as developer tooling integration and data preloading—that extend the core SWR hooks with zero-config usability. By consolidating these into a single preset, the system simplifies middleware management and ensures consistent application of critical features across SWR instances.

The Built-in Middlewares subtopic addresses the need to offer out-of-the-box middleware functionality that complements SWR’s extensibility without requiring users to manually compose or configure each middleware. It strikes a balance between flexibility and convenience by grouping proven, stable middlewares while still allowing additional custom middleware layers if desired.

Functionality

The built-in middleware preset is essentially a curated collection of middleware functions that are composed and applied in sequence to SWR hooks. Two main middleware components are currently included:

The middleware preset exports a single array that concatenates these middlewares:

import { use as devtoolsUse } from './devtools'
import { middleware as preload } from './preload'

export const BUILT_IN_MIDDLEWARE = devtoolsUse.concat(preload)

Workflow

  1. When SWR hooks are instantiated, the built-in middleware array is provided as part of the middleware chain.

  2. Each middleware function wraps or enhances the core hook's behavior, adding its specialized logic.

  3. The devtools middleware attaches event listeners and exposes hook state updates for developer tooling.

  4. The preload middleware manages the prefetching and caching of data before rendering.

  5. Middleware composition ensures that all built-in features are applied in order, forming a seamless enhancement pipeline.

This centralized aggregation prevents duplication and inconsistencies, making it easier to maintain and evolve the middleware set as new features or improvements are introduced.

Integration

The Built-in Middlewares integrate tightly with the Middleware Architecture parent topic by serving as the primary example and default set of middleware layers applied to SWR hooks. They demonstrate how middleware composition can package multiple functionalities into a reusable preset, which users can adopt directly or extend with custom middleware.

By bundling these middlewares, the subtopic exemplifies middleware best practices within the project and serves as a foundation for future middleware development.

Diagram

flowchart LR
    subgraph SWR Hook Initialization
        A[useSWR Core Hook]
        B[Devtools Middleware]
        C[Preload Middleware]
    end

    A --> B --> C --> D[SWR Hook with Built-in Middleware]

This flowchart illustrates the sequential composition of the built-in middlewares wrapping the core SWR hook, resulting in an enhanced hook instance used by components.