middleware-preset.ts
Overview
The middleware-preset.ts file serves as a centralized aggregator and exporter of built-in middleware functions used within the SWR (stale-while-revalidate) data fetching library. Its primary purpose is to bundle essential middleware layers—specifically developer tools integration and data preloading—into a single preset array that can be easily applied to SWR hooks. This preset facilitates the seamless enhancement of core SWR functionality with additional capabilities without requiring users to individually manage or configure each middleware.
Detailed Explanation
Imports
import { use as devtoolsUse } from './devtools'
import { middleware as preload } from './preload'
devtoolsUse: An array of middleware functions related to developer tools integration. These middleware enable real-time inspection, debugging, and synchronization of SWR data states with developer tools.preload: A middleware function that enables preloading of data before React components mount, improving perceived performance by reducing loading waterfalls.
Export
export const BUILT_IN_MIDDLEWARE = devtoolsUse.concat(preload)
BUILT_IN_MIDDLEWARE: A constant array combining all middleware fromdevtoolsUseand thepreloadmiddleware. This array represents the full set of built-in middleware that can be applied collectively as a preset.
Usage Example
To apply the built-in middleware preset in an SWR hook, you could do the following:
import useSWR from 'swr'
import { BUILT_IN_MIDDLEWARE } from './middleware-preset'
const useSWRWithPreset = withMiddleware(useSWR, ...BUILT_IN_MIDDLEWARE)
// Usage in a React component
function Profile() {
const { data, error } = useSWRWithPreset('/api/user', fetcher)
if (error) return <div>Error loading</div>
if (!data) return <div>Loading...</div>
return <div>Hello {data.name}</div>
}
Here, the BUILT_IN_MIDDLEWARE array is spread and applied to the base useSWR hook via a hypothetical withMiddleware function, which wraps the hook with the middleware chain.
Implementation Details
Middleware Composition: Middleware functions in SWR are composable higher-order hooks that enhance or modify the behavior of the core
useSWRhook. This file composes multiple middleware functions by concatenating arrays and exporting them as a single preset.Array Concatenation: The concatenation of
devtoolsUse(an array) withpreload(a single middleware function) results in a flattened array of middleware functions, ensuring correct ordering and execution sequence.Extensibility: By exporting a preset, the system allows users or internal modules to import a standard set of middleware that covers common enhancements, while still allowing flexibility to add or override middleware as needed.
Interaction with the System
Core SWR Hook (
useSWR): This preset is intended to be applied to the coreuseSWRhook or any custom hook built on top of it, enhancing its data fetching lifecycle with developer tooling support and preloading capabilities.Devtools Middleware Module (
./devtools): Provides middleware functions that enable SWR to integrate with developer tools, track cache mutations, and expose internal states.Preload Middleware Module (
./preload): Provides middleware that manages preloading of data, enabling data to be fetched and cached before the component renders.Middleware Architecture: Works as a part of the middleware composition architecture, which layers multiple middleware functions in a chain to extend SWR hooks declaratively.
User Applications: Developers consuming the SWR library can import this preset to effortlessly include these important middleware enhancements without manual configuration.
Mermaid Diagram: Middleware Preset Structure
flowchart LR
subgraph middleware-preset.ts
DT[Devtools Middleware (devtoolsUse)]
PL[Preload Middleware (preload)]
BI[BUILT_IN_MIDDLEWARE Array]
end
DT --> BI
PL --> BI
Description: This flowchart illustrates how the BUILT_IN_MIDDLEWARE array is composed by concatenating the developer tools middleware array (devtoolsUse) with the preload middleware function (preload). The resulting BUILT_IN_MIDDLEWARE is a consolidated array of middleware functions exported for use in SWR hooks.
Summary
middleware-preset.tsprovides a convenient, centralized export of essential middleware functions for SWR.It combines developer tooling middleware (
devtoolsUse) and preloading middleware (preload) into a single preset array.This preset can be applied to the core SWR hook to enable enhanced debugging, inspection, and improved data loading strategies.
The file supports modular middleware architecture, promoting extensibility and maintainability.
Middleware functions wrapped in this preset operate as higher-order hooks, enabling seamless augmentation of the SWR lifecycle.
This simple yet critical file underpins the modular middleware system by packaging commonly used middleware together, simplifying middleware management and adoption across the SWR ecosystem.