# serafimcloud/21st architecture

> Architecture diagram of the GitHub repository serafimcloud/21st, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/serafimcloud/21st
- Repository: https://github.com/serafimcloud/21st

## Overview

##### Project type and purpose
- 21st.dev is a full-stack, open-source registry and community platform for sharing React UI components.
- It combines a Next.js frontend (“apps/web”), a custom bundler service (“apps/backend”), serverless database functions (under “supabase/functions”), shared UI/config packages, and external services for auth, storage, billing, and analytics.

##### File structure
- Top-level “apps” directory:
- apps/web – Next.js 14 application with pages, API routes, UI components, hooks, lib and config.
- apps/backend – A Bun-based microservice that handles bundling, CSS processing, video conversion, and R2 uploads.
- “supabase/functions”: Deno-based serverless functions for embedding, search, notifications, etc.
- “packages”: shared ESLint/TS configs and a UI component library.
- Additional scripts for embedding demos, backups, and database seeds.

##### README architecture section
- Frontend: Next.js 14
- Database: Supabase (PostgreSQL + edge functions)
- Auth: Clerk
- File storage: Cloudflare R2
- Analytics: Amplitude
- Billing: Stripe

a. Frontend (apps/web)
- Next.js pages & API routes
- Sandpack in-browser editor
- Client components and static assets
b. Backend bundler service (apps/backend)
- HTTP entrypoint (serve.ts)
- Bundler layer (Vite integration)
- CSS processor, video converter
- R2 upload module
c. Database & serverless functions (Supabase)
- PostgreSQL metadata store
- Edge functions under “supabase/functions”
d. Shared packages
- UI primitives (packages/ui)
- Config libraries (packages/*-config)
e. External services
- Clerk for authentication
- Cloudflare R2 for code/video storage
- Stripe for subscription/billing
- Amplitude for analytics

##### Relationships and data-flow
- User → Next.js frontend
- Frontend ↔ Supabase (auth, data, edge functions)
- Frontend ↔ apps/backend bundler API
- apps/backend → Cloudflare R2 (storage)
- Supabase edge functions → Supabase DB & R2
- Frontend & backend emit events to Amplitude
- Frontend listens for Stripe webhooks (via API routes)

##### Architectural patterns and principles
- Monorepo with turbo/pnpm workspace – shares code, configs, and types.
- Server-side rendering + API routes in Next.js for public and protected content.
- Dedicated microservice for bundling to isolate long-running tasks.
- Supabase edge functions for lightweight serverless logic co-located with the DB.
- External auth (Clerk) and billing (Stripe) rather than rolling your own.
- Storage-as-a-service (Cloudflare R2) for large assets.

##### Essential details
- Monorepo structure and code sharing packages/ui & config.
- Where environment variables (Clerk keys, R2 credentials) feed into apps/web and apps/backend.
- Third-party webhooks (Stripe webhook routes).
- Call out analytics events path (frontend → Amplitude).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client (Browser)"
        User["User"]:::external
    end

    subgraph "Next.js Server"
        Frontend["Next.js Frontend"]:::inRepo
        Pages["Next.js Pages & API Routes"]:::inRepo
        Sandpack["Sandpack In-Browser Editor"]:::inRepo
        HooksLib["Frontend Hooks & Libs"]:::inRepo
    end

    subgraph "Bundler Service"
        Entrypoint["HTTP Entrypoint (serve.ts)"]:::inRepo
        Bundler["Bundler Layer (Vite Integration)"]:::inRepo
        CSS["CSS Processor"]:::inRepo
        Video["Video Converter"]:::inRepo
        R2Mod["Cloudflare R2 Upload Module"]:::inRepo
        Routes["Backend Routes & Server Setup"]:::inRepo
    end

    subgraph "Supabase"
        DB["PostgreSQL Metadata Store"]:::external
        Functions["Edge Functions"]:::serverless
    end

    subgraph "Shared Packages"
        UI["Shared UI Primitives"]:::inRepo
        ESLintCfg["Shared ESLint Config"]:::inRepo
        TSCfg["Shared TypeScript Config"]:::inRepo
    end

    subgraph "External Services"
        Clerk["Clerk Authentication"]:::external
        R2Store["Cloudflare R2 Storage"]:::external
        Stripe["Stripe Billing"]:::external
        Amplitude["Amplitude Analytics"]:::external
    end

    User -->|"HTTP Request"| Frontend
    Frontend -->|"renders /api & pages"| Pages
    Frontend -->|"loads editor"| Sandpack
    Frontend -->|"uses auth/data/analytics"| HooksLib
    Frontend -->|"build-time imports"| UI
    Frontend -->|"build-time imports"| ESLintCfg
    Frontend -->|"build-time imports"| TSCfg
    Frontend -->|"HTTP API calls"| Entrypoint
    Frontend -->|"API calls /functions"| Functions
    Frontend -->|"emits events"| Amplitude
    Frontend -->|"receives webhooks"| Stripe

    Entrypoint -->|"invokes bundler"| Bundler
    Bundler -->|"processes CSS"| CSS
    Bundler -->|"converts video"| Video
    CSS -->|"uploads assets"| R2Mod
    Video -->|"uploads video"| R2Mod
    R2Mod -->|"writes to"| R2Store
    Entrypoint -->|"build-time imports"| UI
    Entrypoint -->|"build-time imports"| ESLintCfg
    Entrypoint -->|"build-time imports"| TSCfg
    Entrypoint -->|"emits events"| Amplitude

    Functions -->|"reads/writes"| DB
    Functions -->|"reads/writes"| R2Store

    HooksLib --> Clerk
    HooksLib --> R2Store
    HooksLib --> Stripe
    HooksLib --> Amplitude

    click Frontend "https://github.com/serafimcloud/21st/tree/main/apps/web"
    click Pages "https://github.com/serafimcloud/21st/tree/main/apps/web/app"
    click Sandpack "https://github.com/serafimcloud/21st/blob/main/apps/web/lib/sandpack.tsx"
    click Sandpack "https://github.com/serafimcloud/21st/blob/main/apps/web/components/features/studio/editor/sandpack-initial-content.tsx"
    click HooksLib "https://github.com/serafimcloud/21st/tree/main/apps/web/hooks"
    click HooksLib "https://github.com/serafimcloud/21st/tree/main/apps/web/lib"

    click Entrypoint "https://github.com/serafimcloud/21st/blob/main/apps/backend/serve.ts"
    click Bundler "https://github.com/serafimcloud/21st/tree/main/apps/backend/src/bundler"
    click CSS "https://github.com/serafimcloud/21st/blob/main/apps/backend/src/css-processor.ts"
    click Video "https://github.com/serafimcloud/21st/blob/main/apps/backend/src/video-converter.ts"
    click R2Mod "https://github.com/serafimcloud/21st/blob/main/apps/backend/src/r2.ts"
    click Routes "https://github.com/serafimcloud/21st/tree/main/apps/backend/src/routes"
    click Routes "https://github.com/serafimcloud/21st/tree/main/apps/backend/src/server"

    click Functions "https://github.com/serafimcloud/21st/tree/main/supabase/functions"

    click UI "https://github.com/serafimcloud/21st/tree/main/packages/ui"
    click ESLintCfg "https://github.com/serafimcloud/21st/tree/main/packages/eslint-config"
    click TSCfg "https://github.com/serafimcloud/21st/tree/main/packages/typescript-config"

    classDef inRepo fill:#D6EAFE,stroke:#0366D6
    classDef external fill:#E6FFFA,stroke:#1B9AAA
    classDef serverless stroke-dasharray:5 5,fill:none,stroke:#FF9900
```
