Config Routes
By default, Modern.js recommends using Convention Routes as the way to define routes. At the same time, Modern.js also provides a config-based routing capability that can be used together with convention routes or used separately.
When to Use Config Routes
Config routes are particularly useful in the following scenarios:
- Need flexible route control: When the file structure cannot directly map to the desired URL structure
- Multiple routes pointing to the same component: Need to point different URL paths to the same page component
- Conditional routes: Dynamically configure routes based on different conditions
- Legacy project migration: Maintain the original routing structure when migrating from other routing systems
- Complex route naming: Need to customize route paths without being limited by file directory structure
Basic Usage
In the src directory or each entry directory, you can define a modern.routes.ts file to configure routes:
Function Signature Description
defineRoutes accepts a callback function with two parameters:
routeFunctions: An object containingroute,layout,page,$functionsfileRoutes: An array of route configurations generated by convention routes
Basic signature of route functions:
- First parameter: File path relative to
modern.routes.ts - Second parameter: Route path (optional, must be a string)
- Third parameter: Array of child routes (optional)
Route Functions
Modern.js provides four main route configuration functions:
The first parameter (path) of all route functions is a relative path, which will be concatenated with the parent path to generate the final route path:
- Root path:
"/"or""represents the root path of the current level - Relative path: Child route paths will be concatenated relative to the parent path
- Dynamic path: Use
:paramsyntax to represent dynamic parameters - Wildcard path: Use
"*"syntax to match all paths
- The first parameter (component file path) of functions like
route,layout,page,$must point to real files in the current project. Files fromnode_modulesand other repositories in Monorepo are not currently supported. - Path aliases are not supported (such as
@/pages/...,~/pages/..., etc.); please use relative paths relative tomodern.routes.ts.
route Function
The route function is a general-purpose route configuration function that automatically determines whether to generate a page route or layout route based on whether there are child routes. It can replace layout, page, $ and other functions.
Use cases:
- Quick route configuration without explicitly specifying page or layout
- Simplify the complexity of route configuration
layout Function
The layout function is specifically used to configure layout routes, always generates layout components, and must contain child routes:
Use cases:
- Need to explicitly specify a component as a layout component
- Provide common layout structure for multiple pages
- Need to share navigation, sidebar and other UI components across multiple routes
page Function
The page function is specifically used to configure page routes, always generates page components:
Use cases:
- Need to explicitly specify a component as a page component
- Ensure the component does not contain
<Outlet>child component rendering - Provide clearer semantic expression
$ Function
The $ function is specifically used to configure wildcard routes for handling unmatched routes:
Use cases:
- Custom 404 pages
- Handle all unmatched requests under specific paths
The $ function has the same functionality as the $.tsx file in convention routes, used to catch unmatched route requests.
Configuring Routes
Basic Example
Routes Without Path
When no path is specified, routes inherit the parent path:
The above configuration will generate:
/login→auth/layout.tsx+login/page.tsx/register→auth/layout.tsx+register/page.tsx
Multiple Paths Pointing to the Same Component
Dynamic Routes
Config routes support dynamic route parameters:
Automatic Route-Related File Discovery
Config routes automatically discover component-related files without manual configuration. For any component file specified in modern.routes.ts, Modern.js will automatically find the following related files:
Modern.js will automatically find and load:
pages/profile.data.ts→ Data loaderpages/profile.config.ts→ Route configurationpages/profile.error.tsx→ Error boundarypages/profile.loading.tsx→ Loading component
Discovery Rules
- File location: Related files must be in the same directory as the component file
- File naming: Related file names are the same as the component file name (excluding extension)
- Auto-discovery: Modern.js automatically discovers and loads these files
For more detailed information about data fetching, please refer to the Data Fetching documentation. For Loading component usage, please refer to Convention Routes - Loading.
Route Merging
Config routes can be used together with convention routes. You can merge routes by modifying the fileRoutes parameter:
- Override routes: You can actively remove convention routes and replace them with config routes
- Supplement routes: You can add new config routes based on convention routes
- Mixed usage: You can add config child routes under convention layout routes
Current route structure can be viewed through the modern routes command
Merging Examples
The following examples demonstrate how to merge config routes with convention routes:
Debugging Routes
Since the final structure after route merging may not be intuitive, Modern.js provides debugging commands to help you view complete route information:
This command will generate the final route structure in the dist/routes-inspect.json file, helping you understand the complete route information after merging.
Report File Examples
Single Entry Scenario
Multi-entry Scenario
In multi-entry projects, the report file will be grouped by entry name, where the key is entryName:
Route-Related File Field Descriptions
In addition to basic route information, the report also displays related files found for each route:
data: Server-side data loading file (.data.ts), used to fetch data on the serverclientData: Client-side data loading file (.data.client.ts), used to refetch data on the clienterror: Error boundary file (.error.tsx), used to handle route rendering errorsloading: Loading state component file (.loading.tsx), used to display data loading stateconfig: Route configuration file (.config.ts), used to configure route metadata
These fields are optional and will only be displayed in the report when corresponding files are found. By viewing these fields, you can quickly understand the complete file structure for each route.