File-based Routing
Relevant source files
The following files were used as context for generating this wiki page:
File-based routing in OpenChat is implemented using TanStack Router and TanStack Start. This architecture allows the application to define its routing structure through the physical directory layout of the src/routes/ folder. The system supports full-stack capabilities, including Server-Side Rendering (SSR) for UI components and dedicated API routes for backend logic.
The routing system is type-safe, with a generated route tree that ensures all links and navigation parameters are validated at compile-time. It handles various route types, including layout routes (prefixed with _), dynamic path segments (prefixed with $), and API-specific endpoints.
Sources: CLAUDE.md:14, CLAUDE.md:65-68, src/routeTree.gen.ts:15-50
Architecture and Route Types
The project utilizes a hierarchical routing structure managed by TanStack Router. This structure is categorized into three primary types of routes: UI routes, Layout/Group routes, and API routes.
Route Classification
| Route Type | Description | Naming Convention | Example |
|---|---|---|---|
| Root Route | The shell pattern for the entire application. | __root.tsx | src/routes/__root.tsx |
| API Routes | Server-side endpoints for data mutations and AI interactions. | api.*.ts | src/routes/api.chat.ts |
| Layout Routes | Pathless routes used to wrap multiple children in a shared layout. | _layoutName/ | src/routes/_chat/route.tsx |
| Dynamic Routes | Routes that accept URL parameters. | $paramName | src/routes/share.$chatId.tsx |
| Static Routes | Standard public pages. | name.tsx | src/routes/privacy.tsx |
Sources: CLAUDE.md:65-68, src/routeTree.gen.ts:25-50
The Route Tree Generation
The src/routeTree.gen.ts file acts as the central registry for all routes. It is automatically generated and should not be modified manually. It imports route definitions and organizes them into a tree structure that the router uses to match incoming URLs.
The diagram above illustrates the hierarchical relationship between routes as defined in the generated route tree.
Sources: src/routeTree.gen.ts:1-200
API Route Implementation
API routes are defined as .ts files within the src/routes/ directory. These routes handle server-side logic such as AI message streaming, rate limiting, and database mutations via the Convex client.
Case Study: /api/chat
The chat API route (src/routes/api.chat.ts) demonstrates the integration of routing with AI SDKs and authentication middleware.
- Middleware: Uses
authMiddlewareto ensure requests are authenticated. - Handlers: Defines a
POSThandler to process chat messages. - Context: Accesses the Convex client and authentication token through the
contextobject provided by the router.
Sources: src/routes/api.chat.ts:608-615, src/routes/api.create-chat.ts:19-22
API Workflow Sequence
The flow shows how an API route request transitions from a client call through routing middleware to final execution.
Sources: src/routes/api.chat.ts:608-650, src/routes/api.create-chat.ts:25-45
UI Route Implementation and Security
UI routes are created using the createFileRoute function. These routes can specify whether they require Server-Side Rendering (SSR) and define the component to be rendered.
Navigation and Protection
Routes such as /tasks implement client-side protection by checking the user's state within the component and navigating away if the user is unauthenticated.
// src/routes/tasks.tsx:13-22
function TasksPage() {
const { user, isLoading } = useUser();
const router = useRouter();
useEffect(() => {
if (isLoading) return;
if (!user || user.isAnonymous) {
void router.navigate({ to: "/auth" });
}
}, [user, isLoading, router]);
// ...
}
Route Configuration Examples
| File | Path | SSR Enabled | Purpose |
|---|---|---|---|
src/routes/security.tsx | /security | Yes | Legal/Security policy display |
src/routes/terms.tsx | /terms | Yes | Terms of Service display |
src/routes/tasks.tsx | /tasks | No | Scheduled tasks management |
Sources: src/routes/security.tsx:4-7, src/routes/terms.tsx:4-7, src/routes/tasks.tsx:7-9
Conclusion
OpenChat leverages a robust file-based routing system that unifies frontend components and backend API endpoints under a single directory structure. By using TanStack Router, the project achieves a high degree of type safety and developer productivity, ensuring that the route tree stays synchronized with the filesystem. This architecture facilitates complex patterns like layout nesting, dynamic parameters for shared chats, and secure server-side handlers for AI-driven features.