# blockscout/frontend architecture

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

- Interactive diagram: https://gitdiagram.com/blockscout/frontend
- Repository: https://github.com/blockscout/frontend

## Overview

##### Project type and purpose
- This is a Next.js-based frontend application for Blockscout, an open-source blockchain explorer.
- It provides server-side rendering (SSR) and static site generation (SSG) of pages that display blocks, transactions, addresses, tokens, L2 batches/withdrawals, user operations, contract verification, analytics dashboards, etc.

##### File structure
- Top-level directories:
- pages/ – Next.js pages (SSR/SSG, API routes under pages/api)
- nextjs/ – Next.js custom routing, CSP, middleware, headers, rewrites/redirects
- ui/ & toolkit/ – React components and design system (Chakra UI wrappers, shared widgets)
- lib/ – Low-level API clients, hooks, utilities, socket contexts, feature-flag integration
- configs/ – Environment and feature-flag configuration, per-network ENVs
- deploy/ & .github/workflows – CI/CD and deployment manifests (Dockerfile, Helmfile, GitHub Actions)
- Patterns:
- Hybrid SSR/CSR with Next.js page conventions
- Feature flags via GrowthBook and configs/app/features
- Modular “ui” and “toolkit” libraries for shared components
- API-first design: lib/api contains fetch hooks and service wrappers

##### README insights
- Distributed as a Docker image; configured entirely via environment variables.
- Backend is decoupled: frontend calls the Blockscout REST/GraphQL APIs and WebSocket endpoints exposed by the Blockscout backend (not part of this repo).
- Supports custom builds, local development via devcontainer, and multiple network environments.

##### Details to capture
- Next.js page lifecycle: getServerSideProps, getStaticProps, client fetch hooks
- API proxy layer under pages/api
- WebSocket context usage for real-time notifications
- Feature flag evaluation at server and client
- Static asset flow: build → public folder → CDN

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Browser["Browser / Client\n(React hydration & CSR)"]:::client
    UIkit["UI Component Libraries\n(ui & toolkit)"]:::client
    Browser -->|"uses components"| UIkit
    Browser -->|"SSR/CSR requests"| NextServer

    %% Server Layer
    NextServer["Next.js Server\n(SSR/SSG & API proxy)"]:::server
    WebSocketClient["WebSocket Client & Context\n(live updates)"]:::server
    Browser -->|"WebSocket connect"| WebSocketClient
    WebSocketClient -->|"subscriptions"| WebSocketServer
    NextServer -->|"API proxy & data fetch"| BackendAPI
    NextServer -->|"serves assets"| CDN
    NextServer -->|"feature evaluation"| FeatureFlags
    NextServer -->|"logs & events"| Analytics

    %% External Services
    BackendAPI["Blockscout Backend API\n(REST/GraphQL)"]:::external
    WebSocketServer["WebSocket Server"]:::external
    FeatureFlags["Feature Flags\n(GrowthBook)"]:::external
    Analytics["Analytics & Error Monitoring"]:::external
    NodeProviders["Blockchain Node Providers"]:::external

    %% Data Flow
    BackendAPI -->|"fetches raw data"| NodeProviders

    %% CDN
    CDN["Static Asset Store / CDN"]:::cdn

    %% Deployment Pipeline
    CICD["CI/CD & Deployment"]:::deploy
    Kubernetes["Kubernetes Cluster"]:::deploy
    CICD -->|"build & push Docker image"| Kubernetes

    %% Click Events for component mapping
    click Browser "https://github.com/blockscout/frontend/blob/main/pages/_app.tsx"
    click NextServer "https://github.com/blockscout/frontend/blob/main/pages/api/proxy.ts"
    click NextServer "https://github.com/blockscout/frontend/blob/main/nextjs/getServerSideProps.ts"
    click NextServer "https://github.com/blockscout/frontend/blob/main/next.config.js"
    click NextServer "https://github.com/blockscout/frontend/tree/main/nextjs/middlewares/"
    click CDN "https://github.com/blockscout/frontend/tree/main/public/static/"
    click BackendAPI "https://github.com/blockscout/frontend/tree/main/lib/api/services/"
    click WebSocketClient "https://github.com/blockscout/frontend/blob/main/lib/socket/context.tsx"
    click FeatureFlags "https://github.com/blockscout/frontend/tree/main/configs/app/features/"
    click FeatureFlags "https://github.com/blockscout/frontend/tree/main/lib/growthbook/"
    click Analytics "https://github.com/blockscout/frontend/tree/main/lib/mixpanel/"
    click Analytics "https://github.com/blockscout/frontend/tree/main/lib/rollbar/"
    click Analytics "https://github.com/blockscout/frontend/blob/main/nextjs/csp/policies/googleAnalytics.ts"
    click Analytics "https://github.com/blockscout/frontend/blob/main/nextjs/csp/policies/rollbar.ts"
    click UIkit "https://github.com/blockscout/frontend/tree/main/ui/"
    click UIkit "https://github.com/blockscout/frontend/tree/main/toolkit/"
    click CICD "https://github.com/blockscout/frontend/tree/main/.github/workflows/"
    click CICD "https://github.com/blockscout/frontend/tree/main/Dockerfile"
    click CICD "https://github.com/blockscout/frontend/blob/main/deploy/helmfile.yaml"

    %% Styling
    classDef client fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef server fill:#d4edda,stroke:#333,stroke-width:1px
    classDef external fill:#ffe5b4,stroke:#333,stroke-width:1px
    classDef cdn fill:#f0e68c,stroke:#333,stroke-width:1px
    classDef deploy fill:#d3d3d3,stroke:#333,stroke-width:1px
```
