Skip to content

Commit c83d7af

Browse files
arvinxxclaude[bot]
andauthored
📝 docs: update app directory structure documentation (lobehub#9582)
- Update folder-structure.mdx and zh-CN version to reflect current Next.js 13+ App Router architecture - Replace outdated simple desktop/mobile structure with actual complex structure - Add documentation for (backend), [variants], @modal, and desktop route groups - Include API architecture explanation with tRPC and REST endpoints - Document platform organization and deployment targets Fixes lobehub#9522 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Arvin Xu <[email protected]>
1 parent 210a41b commit c83d7af

File tree

2 files changed

+134
-32
lines changed

2 files changed

+134
-32
lines changed

docs/development/basic/folder-structure.mdx

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,88 @@ The directory structure of LobeChat is as follows:
44

55
```bash
66
src
7-
├── app # Main logic and state management related code for the application
7+
├── app # Next.js App Router implementation with route groups and API routes
88
├── components # Reusable UI components
99
├── config # Application configuration files, including client-side and server-side environment variables
10-
├── const # Used to define constants, such as action types, route names, etc.
1110
├── features # Function modules related to business functions, such as agent settings, plugin development pop-ups, etc.
1211
├── hooks # Custom utility hooks reused throughout the application
1312
├── layout # Application layout components, such as navigation bars, sidebars, etc.
13+
├── libs # Third-party integrations (analytics, OIDC, etc.)
1414
├── locales # Internationalization language files
15+
├── server # Server-side modules and services
1516
├── services # Encapsulated backend service interfaces, such as HTTP requests
1617
├── store # Zustand store for state management
18+
├── styles # Global styles and CSS-in-JS configurations
1719
├── types # TypeScript type definition files
1820
└── utils # Common utility functions
1921
```
2022

2123
## app
2224

23-
In the `app` folder, we organize each route page according to the app router's [Route Groups](https://nextjs.org/docs/app/building-your-application/routing/route-groups) to separately handle the implementation of desktop and mobile code. Taking the file structure of the `welcome` page as an example:
25+
The `app` directory follows Next.js 13+ App Router conventions with a sophisticated architecture using [Route Groups](https://nextjs.org/docs/app/building-your-application/routing/route-groups) to organize backend services, platform variants, and application routes:
2426

2527
```bash
26-
welcome
27-
├── (desktop) # Desktop implementation
28-
│ ├── features # Desktop-specific features
29-
│ ├── index.tsx # Main entry file for desktop
30-
│ └── layout.desktop.tsx # Desktop layout component
31-
├── (mobile) # Mobile implementation
32-
│ ├── features # Mobile-specific features
33-
│ ├── index.tsx # Main entry file for mobile
34-
│ └── layout.mobile.tsx # Mobile layout component
35-
├── features # This folder contains features code shared by both desktop and mobile, such as the Banner component
36-
│ └── Banner
37-
└── page.tsx # This is the main entry file for the page, used to load desktop or mobile code based on the device type
28+
app
29+
├── (backend)/ # Backend API routes and services
30+
│ ├── api/ # REST API endpoints
31+
│ │ ├── auth/ # Authentication routes
32+
│ │ └── webhooks/ # Webhook handlers
33+
│ ├── middleware/ # Request middleware
34+
│ ├── oidc/ # OpenID Connect routes
35+
│ ├── trpc/ # tRPC API endpoints
36+
│ │ ├── async/ # Async tRPC routes
37+
│ │ ├── desktop/ # Desktop-specific tRPC routes
38+
│ │ ├── edge/ # Edge runtime tRPC routes
39+
│ │ ├── lambda/ # Lambda tRPC routes
40+
│ │ └── tools/ # Tools tRPC routes
41+
│ └── webapi/ # Web API endpoints
42+
│ ├── chat/ # Chat-related APIs
43+
│ ├── models/ # Model management APIs
44+
│ ├── tts/ # Text-to-speech APIs
45+
│ └── ...
46+
├── [variants]/ # Platform and device variants
47+
│ ├── (auth)/ # Authentication pages
48+
│ │ ├── login/
49+
│ │ ├── signup/
50+
│ │ └── next-auth/
51+
│ ├── (main)/ # Main application routes
52+
│ │ ├── (mobile)/ # Mobile-specific routes
53+
│ │ │ └── me/ # Mobile profile pages
54+
│ │ ├── _layout/ # Layout components
55+
│ │ ├── chat/ # Chat interface
56+
│ │ ├── discover/ # Discovery pages
57+
│ │ ├── files/ # File management
58+
│ │ ├── image/ # Image generation
59+
│ │ ├── profile/ # User profile
60+
│ │ ├── repos/ # Repository management
61+
│ │ └── settings/ # Application settings
62+
│ └── @modal/ # Parallel modal routes
63+
│ ├── (.)changelog/
64+
│ └── _layout/
65+
├── desktop/ # Desktop-specific routes
66+
│ └── devtools/
67+
├── manifest.ts # PWA manifest
68+
├── robots.tsx # Robots.txt generation
69+
├── sitemap.tsx # Sitemap generation
70+
└── sw.ts # Service worker
3871
```
3972

40-
In this way, we can clearly distinguish and manage desktop and mobile code, while also easily reusing code required on both devices, thereby improving development efficiency and maintaining code cleanliness and maintainability.
73+
### Architecture Explanation
74+
75+
**Route Groups:**
76+
- `(backend)` - Contains all server-side API routes, middleware, and backend services
77+
- `[variants]` - Dynamic route group handling different platform variants and main application pages
78+
- `@modal` - Parallel routes for modal dialogs using Next.js parallel routing
79+
80+
**Platform Organization:**
81+
- The architecture supports multiple platforms (web, desktop, mobile) through route organization
82+
- Desktop-specific routes are in the `desktop/` directory
83+
- Mobile-specific routes are organized under `(main)/(mobile)/`
84+
- Shared layouts and components are in `_layout/` directories
85+
86+
**API Architecture:**
87+
- REST APIs in `(backend)/api/` and `(backend)/webapi/`
88+
- tRPC endpoints organized by runtime environment (edge, lambda, async, desktop)
89+
- Authentication and OIDC handling in dedicated route groups
90+
91+
This architecture provides clear separation of concerns while maintaining flexibility for different deployment targets and runtime environments.

docs/development/basic/folder-structure.zh-CN.mdx

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,88 @@ LobeChat 的文件夹目录架构如下:
44

55
```bash
66
src
7-
├── app # 应用主要逻辑和状态管理相关的代码
7+
├── app # Next.js App Router 实现,包含路由组和 API 路由
88
├── components # 可复用的 UI 组件
99
├── config # 应用的配置文件,包含客户端环境变量与服务端环境变量
10-
├── const # 用于定义常量,如 action 类型、路由名等
1110
├── features # 与业务功能相关的功能模块,如 Agent 设置、插件开发弹窗等
1211
├── hooks # 全应用复用自定义的工具 Hooks
1312
├── layout # 应用的布局组件,如导航栏、侧边栏等
13+
├── libs # 第三方集成(分析、OIDC 等)
1414
├── locales # 国际化的语言文件
15+
├── server # 服务端模块和服务
1516
├── services # 封装的后端服务接口,如 HTTP 请求
1617
├── store # 用于状态管理的 zustand store
18+
├── styles # 全局样式和 CSS-in-JS 配置
1719
├── types # TypeScript 的类型定义文件
1820
└── utils # 通用的工具函数
1921
```
2022

2123
## app
2224

23-
`app` 文件夹中,我们将每个路由页面按照 app router 的 [Route Groups](https://nextjs.org/docs/app/building-your-application/routing/route-groups) 进行组织,以此来分别处理桌面端和移动端的代码实现。以 `welcome` 页面的文件结构为例
25+
`app` 目录遵循 Next.js 13+ App Router 约定,采用复杂的架构,使用 [路由组](https://nextjs.org/docs/app/building-your-application/routing/route-groups) 来组织后端服务、平台变体和应用路由
2426

2527
```bash
26-
welcome
27-
├── (desktop) # 桌面端实现
28-
│ ├── features # 桌面端特有的功能
29-
│ ├── index.tsx # 桌面端的主入口文件
30-
│ └── layout.desktop.tsx # 桌面端的布局组件
31-
├── (mobile) # 移动端实现
32-
│ ├── features # 移动端特有的功能
33-
│ ├── index.tsx # 移动端的主入口文件
34-
│ └── layout.mobile.tsx # 移动端的布局组件
35-
├── features # 此文件夹包含双端共享的特性代码,如 Banner 组件
36-
│ └── Banner
37-
└── page.tsx # 此为页面的主入口文件,用于根据设备类型选择加载桌面端或移动端的代码
28+
app
29+
├── (backend)/ # 后端 API 路由和服务
30+
│ ├── api/ # REST API 端点
31+
│ │ ├── auth/ # 身份验证路由
32+
│ │ └── webhooks/ # Webhook 处理器
33+
│ ├── middleware/ # 请求中间件
34+
│ ├── oidc/ # OpenID Connect 路由
35+
│ ├── trpc/ # tRPC API 端点
36+
│ │ ├── async/ # 异步 tRPC 路由
37+
│ │ ├── desktop/ # 桌面端专用 tRPC 路由
38+
│ │ ├── edge/ # Edge 运行时 tRPC 路由
39+
│ │ ├── lambda/ # Lambda tRPC 路由
40+
│ │ └── tools/ # 工具 tRPC 路由
41+
│ └── webapi/ # Web API 端点
42+
│ ├── chat/ # 聊天相关 API
43+
│ ├── models/ # 模型管理 API
44+
│ ├── tts/ # 文本转语音 API
45+
│ └── ...
46+
├── [variants]/ # 平台和设备变体
47+
│ ├── (auth)/ # 身份验证页面
48+
│ │ ├── login/
49+
│ │ ├── signup/
50+
│ │ └── next-auth/
51+
│ ├── (main)/ # 主应用路由
52+
│ │ ├── (mobile)/ # 移动端专用路由
53+
│ │ │ └── me/ # 移动端个人资料页面
54+
│ │ ├── _layout/ # 布局组件
55+
│ │ ├── chat/ # 聊天界面
56+
│ │ ├── discover/ # 发现页面
57+
│ │ ├── files/ # 文件管理
58+
│ │ ├── image/ # 图像生成
59+
│ │ ├── profile/ # 用户资料
60+
│ │ ├── repos/ # 仓库管理
61+
│ │ └── settings/ # 应用设置
62+
│ └── @modal/ # 并行模态框路由
63+
│ ├── (.)changelog/
64+
│ └── _layout/
65+
├── desktop/ # 桌面端专用路由
66+
│ └── devtools/
67+
├── manifest.ts # PWA 清单
68+
├── robots.tsx # Robots.txt 生成
69+
├── sitemap.tsx # 站点地图生成
70+
└── sw.ts # Service Worker
3871
```
3972

40-
通过这种方式,我们可以清晰地区分和管理桌面端和移动端的代码,同时也能方便地复用在两种设备上都需要的代码,从而提高开发效率并保持代码的整洁和可维护性。
73+
### 架构说明
74+
75+
**路由组:**
76+
- `(backend)` - 包含所有服务端 API 路由、中间件和后端服务
77+
- `[variants]` - 处理不同平台变体和主应用页面的动态路由组
78+
- `@modal` - 使用 Next.js 并行路由的模态框对话框并行路由
79+
80+
**平台组织:**
81+
- 架构通过路由组织支持多个平台(Web、桌面端、移动端)
82+
- 桌面端专用路由位于 `desktop/` 目录中
83+
- 移动端专用路由组织在 `(main)/(mobile)/`
84+
- 共享布局和组件位于 `_layout/` 目录中
85+
86+
**API 架构:**
87+
- `(backend)/api/``(backend)/webapi/` 中的 REST API
88+
- 按运行时环境组织的 tRPC 端点(edge、lambda、async、desktop)
89+
- 专用路由组中的身份验证和 OIDC 处理
90+
91+
这种架构在保持不同部署目标和运行时环境灵活性的同时,提供了清晰的关注点分离。

0 commit comments

Comments
 (0)