# 47ng/nuqs architecture

> Architecture diagram of the GitHub repository 47ng/nuqs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/47ng/nuqs
- Repository: https://github.com/47ng/nuqs

## Overview

##### Main Components Identification

The system consists of several key components:
- Core Library (nuqs)
- Framework Adapters
- Parser System
- State Management
- URL Management
- Server-Side Integration

**┌─ Application Layer ──────────────────────────────────────┐**

- ┌─ Framework Adapters ─────────────────────────────┐   │
- Next.js | React Router | Remix | Plain React     │   │
- ┘   │

- ┌─ Core Components ──────────────────────────────────┐ │

- ┌─ State Management ─┐    ┌─ URL Management ─┐   │ │
- useQueryState      │    │ History API      │   │ │
- useQueryStates     │◄──►│ URL Parsing      │   │ │
- Cache System       │    │ Serialization    │   │ │
- ┘    └────────────────--┘   │ │

- ┌─ Parser System ────┐    ┌─ Server Side ────┐   │ │
- Built-in Parsers   │    │ SSR Support      │   │ │
- Custom Parsers     │    │ Server Cache     │   │ │
- Type Inference     │    │ Loaders         │   │ │
- ┘    └────────────────--┘   │ │

- ┘ │

- ┘

##### Data Flow Indicators

- Bidirectional flow between State Management and URL Management
- One-way flow from Parser System to State Management
- Integration points between Server Side and Core Components

##### Component Details

For each major component, include:

Framework Adapters:
- Next.js (App & Pages Router)
- React Router (v6 & v7)
- Remix
- Plain React
- Testing Adapter

State Management:
- useQueryState hook
- useQueryStates hook
- Cache system
- State synchronization

Parser System:
- Built-in parsers (string, integer, float, boolean, etc.)
- Custom parser support
- Type inference system
- Serialization helpers

URL Management:
- History API integration
- URL parsing and formatting
- Throttling mechanism
- Batch updates

Server-Side Features:
- Server component support
- SearchParams cache
- Loader functions
- SSR compatibility

##### Additional Elements

Include:
- Version compatibility notes
- Integration points with React's features (useTransition, Suspense)
- Error handling flow
- Testing integration points

##### Notes for Implementation

- Type safety throughout the system
- Framework independence of core functionality
- Extensibility through custom parsers
- Performance optimizations (throttling, batching)
- Server/client boundary handling

This system design accurately represents nuqs as a sophisticated state management library that bridges URL-based state with React's component model while maintaining type safety and framework flexibility.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Application
        subgraph Frameworks
            Next["Next.js"]:::framework
            Router["React Router"]:::framework
            Remix["Remix"]:::framework
            React["Plain React"]:::framework
        end

        subgraph Core["Core Components"]
            subgraph StateManagement["State Management"]
                QueryState["useQueryState"]:::state
                QueryStates["useQueryStates"]:::state
                Cache["Cache System"]:::state
            end

            subgraph URLManagement["URL Management"]
                History["History API"]:::url
                URLParsing["URL Parsing"]:::url
                UpdateQueue["Update Queue"]:::url
            end

            subgraph ParserSystem["Parser System"]
                Parsers["Built-in Parsers"]:::parser
                CustomParsers["Custom Parsers"]:::parser
                TypeInference["Type Inference"]:::parser
            end

            subgraph ServerSide["Server-Side Integration"]
                SSR["SSR Support"]:::server
                ServerCache["Server Cache"]:::server
                Loader["Loaders"]:::server
            end
        end
    end

    %% Relationships
    Frameworks --> Core
    StateManagement <--> URLManagement
    ParserSystem --> StateManagement
    ServerSide <--> Core

    %% Click Events
    click Next "https://github.com/47ng/nuqs/tree/next/packages/nuqs/src/adapters/next/"
    click Router "https://github.com/47ng/nuqs/tree/next/packages/nuqs/src/adapters/react-router/"
    click Remix "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/adapters/remix.ts"
    click React "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/adapters/react.ts"
    click QueryState "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/useQueryState.ts"
    click QueryStates "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/useQueryStates.ts"
    click Cache "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/cache.ts"
    click History "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/adapters/lib/patch-history.ts"
    click URLParsing "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/url-encoding.ts"
    click UpdateQueue "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/update-queue.ts"
    click Parsers "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/parsers.ts"
    click TypeInference "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/tests/parsers.test-d.ts"
    click Loader "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/loader.ts"
    click SSR "https://github.com/47ng/nuqs/blob/next/packages/nuqs/src/index.server.ts"
    click ServerCache "https://github.com/47ng/nuqs/blob/next/packages/nuqs/server.d.ts"

    %% Styling
    classDef framework fill:#3498db,stroke:#2980b9,color:white
    classDef state fill:#2ecc71,stroke:#27ae60,color:white
    classDef url fill:#e67e22,stroke:#d35400,color:white
    classDef parser fill:#9b59b6,stroke:#8e44ad,color:white
    classDef server fill:#f1c40f,stroke:#f39c12,color:black

    %% Legend
    subgraph Legend
        Framework["Framework Adapters"]:::framework
        State["State Management"]:::state
        URL["URL Management"]:::url
        Parser["Parser System"]:::parser
        Server["Server-Side"]:::server
    end
```
