Developer Tooling and Debugging

This module enhances the developer experience by providing tools to trace and inspect the internal state changes of the SWR data fetching library. It integrates devtools hooks for live inspection and offers a debug history hook that logs state evolution over time, facilitating easier troubleshooting and performance tuning during development.


Overview

The Developer Tooling and Debugging topic centers on two key capabilities:

Both tools aim to provide transparent visibility into the state management and data flows within the SWR ecosystem without impacting production builds.


Devtools Integration

Purpose

The devtools integration exists to expose SWR's internal lifecycle and cache state to an external interface accessible via browser devtools or a dedicated panel. This helps developers monitor cache entries, revalidation events, mutations, and subscription updates in real time.

How It Works

Code Reference

// src/_internal/utils/devtools.ts

const enableDevtools = isWindowDefined && window.__SWR_DEVTOOLS_USE__

export const use = enableDevtools
  ? window.__SWR_DEVTOOLS_USE__
  : []

export const setupDevTools = () => {
  if (enableDevtools) {
    window.__SWR_DEVTOOLS_REACT__ = React
  }
}

Interaction with Other Modules


Debug History Hook

Purpose

The debug history hook provides a lightweight mechanism within React components to track and display the chronological history of a piece of state or data. This is useful for developers to observe the sequence of data changes without setting up complex logging or external devtools.

How It Works

Code Reference

// e2e/site/lib/use-debug-history.ts

export const useDebugHistory = <T>(data: T, prefix = '') => {
  const dataRef = useRef<T[]>([])
  const debugRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    dataRef.current.push(data)
    if (debugRef.current) {
      debugRef.current.innerText = `${prefix}${JSON.stringify(dataRef.current)}`
    }
  }, [data, prefix])

  return debugRef
}

Interaction with Other Modules


Summary of Interactions


Visualization

The following sequence diagram illustrates the developer workflow when using these debugging tools during SWR hook usage:

sequenceDiagram
  participant Dev as Developer
  participant Comp as React Component
  participant SWR as useSWR Hook
  participant Devtools as SWR Devtools Integration
  participant DebugHook as Debug History Hook

  Dev->>Comp: Mount component using useSWR
  Comp->>SWR: Fetch and cache data
  SWR->>Devtools: Emit cache & state events (if enabled)
  SWR->>Comp: Provide data and revalidation handlers
  Comp->>DebugHook: Pass current data state
  DebugHook->>Comp: Render data history in UI
  Dev->>Devtools: Inspect cache and events via devtools UI
  Dev->>Comp: Observe data history changes inline

This documentation explains how the Developer Tooling and Debugging module empowers developers with real-time data inspection and history tracking capabilities to enhance observability and troubleshooting in the SWR data fetching library.