global-swr-config.tsx
Overview
The global-swr-config.tsx file provides a centralized configuration for the SWR (stale-while-revalidate) data fetching library within a React application. Its primary purpose is to wrap the React component tree with a global SWR configuration context that:
Specifies a custom
fetcherfunction to be used for data fetching.Enables React Suspense mode for handling asynchronous data fetching UI states.
By doing so, it standardizes the data fetching behavior across the entire app and allows components consuming SWR hooks (useSWR) to benefit from consistent configuration without needing to specify these settings individually.
Detailed Explanation
Imports
'use client'
import { SWRConfig } from 'swr'
import fetcher from './libs/fetch'
'use client'directive indicates this file is intended to run in a client-side React environment (Next.js or similar).SWRConfigis the context provider component from the SWR library that allows global configuration.fetcheris a custom function imported from a local utility module (./libs/fetch), responsible for performing network requests (e.g., usingfetchAPI).
Module Declaration Augmentation
declare module 'swr' {
interface SWRGlobalConfig {
suspense: true
}
}
This TypeScript declaration merges with the existing SWR module types.
It explicitly extends the
SWRGlobalConfiginterface to includesuspense: true.This ensures TypeScript recognizes
suspenseas a valid global SWR config option and enforces that it is alwaystruein this app context.
GlobalSWRConfig Component
export function GlobalSWRConfig({ children }: { children: React.ReactNode }) {
return (
<SWRConfig
value={{
fetcher,
suspense: true
}}
>
{children}
</SWRConfig>
)
}
Purpose
This React functional component serves as a wrapper to provide global SWR configuration to all child components.
It leverages
SWRConfigfrom theswrlibrary and passes a configuration object as itsvalueprop.
Parameters
Name | Type | Description |
|---|---|---|
children |
| The React component subtree to which the config applies. |
Behavior
The
fetcherset here is the default function SWR calls internally to fetch data.suspense: trueenables React Suspense mode for SWR, allowing components to suspend rendering while waiting for data.Any component inside this provider using SWR hooks inherits this configuration automatically.
Usage Example
import { GlobalSWRConfig } from './global-swr-config'
function App() {
return (
<GlobalSWRConfig>
<YourAppComponents />
</GlobalSWRConfig>
)
}
In this example, all components inside <YourAppComponents /> can use useSWR without needing to specify fetcher or suspense options explicitly.
Important Implementation Details
React Suspense Mode: Enabling suspense in SWR means that SWR hooks will throw a Promise while fetching data, which React Suspense boundaries catch to display fallback content. This simplifies handling loading states but requires React components to be wrapped with
<Suspense>components higher up.Custom Fetcher: The centralized fetcher function (imported from
./libs/fetch) abstracts the underlying API call mechanism, potentially adding features like error handling, headers, or parsing responses. This helps maintain a single source of truth for how data is fetched.TypeScript Module Augmentation: Extending SWR's
SWRGlobalConfiginterface ensures type safety and developer experience consistency when using thesuspenseoption globally.
Interaction with Other Parts of the System
This file interacts primarily with:
./libs/fetch: The custom fetcher function is imported from this utility module. Changes in the fetcher implementation directly affect all SWR data fetching.React Components: Any React component within the app that uses SWR hooks (
useSWR) will consume this global configuration implicitly.React Suspense Boundaries: Since suspense mode is enabled globally, root components or layout components should include
<Suspense>boundaries to handle loading states gracefully.
The file does not perform any data fetching itself; it only provides configuration.
Summary
Feature | Description |
|---|---|
Global SWR Config | Centralized SWR settings for fetcher and suspense mode |
Suspense Enabled | Allows React Suspense for data fetching UI |
Custom Fetcher | Uses application-specific fetch implementation |
TypeScript Safety | Module augmentation for correct typings |
React Context Provider | Wraps app components to inject config |
Mermaid Component Diagram
componentDiagram
component GlobalSWRConfig {
+children: React.ReactNode
+returns: JSX.Element
}
component SWRConfig {
+value: SWRGlobalConfig
}
component fetcher {
<<function>>
+input: request info
+output: Promise<Response>
}
GlobalSWRConfig --> SWRConfig : wraps
SWRConfig ..> fetcher : uses as fetcher
GlobalSWRConfig --> children : renders children inside