Skip to main content

File-Based Routing Conventions

While React Router v7 recommends manual route configuration, the @react-router/fs-routes package provides file-based routing conventions for those who prefer convention over configuration.

Setup

Install the package:
Use flatRoutes() in your routes.ts:
This automatically discovers routes from the app/routes/ directory.

Basic File Naming

Simple Routes

File names map directly to URL paths:

Nested Routes with Dot Notation

Use dots (.) to create nested routes:
The parent route (dashboard.tsx) must render an <Outlet /> for children.

Dynamic Segments

Prefix segments with $ to create dynamic parameters:
Access params in your component:

Index Routes

Use _index suffix for index routes:

Layout Routes (Pathless)

Prefix with _ to create layout routes that don’t add URL segments:
Both _marketing.tsx and _app.tsx are layout routes that wrap their children without adding URL segments.

Folder-Based Routes

Alternatively, use folders with route.tsx or index.tsx:
Note: Don’t mix route.tsx and file name in the same folder:

Escaping Special Characters

Use square brackets [] to escape special characters:
Escaped characters are treated literally in the URL.

Optional Segments

Wrap segments in parentheses () to make them optional:
Optional segments create multiple route patterns:

Splat Routes (Catch-All)

Use $ alone to match remaining path segments:
Access the splat value via params["*"]:

Escaping Routes (Opt-Out of Nesting)

Use trailing underscore _ to escape parent layout:
Double underscore to skip multiple levels:

Combining Conventions

Mix file and folder approaches:

Ignored Files

Files matching these patterns are ignored:
  • .DS_Store
  • Any file starting with . (hidden files)
  • Files matching patterns in the ignoredRouteFiles config

Custom Routes Directory

Change the routes directory:

Route Conflicts

React Router detects and warns about route conflicts:
⚠️ Order matters! users.new.tsx should be defined before users.$id.tsx to match /users/new correctly. React Router automatically handles this when using flatRoutes().

Complete Example

A real-world file structure:

Migration Strategy

When migrating from manual config to file-based routing:
  1. Start with existing routes.ts manual configuration
  2. Gradually move routes to file-based conventions
  3. Mix both approaches during migration:

Best Practices

  1. Consistent structure: Choose file or folder approach and stick with it
  2. Descriptive names: Use clear, semantic file names
  3. Logical grouping: Group related routes in folders
  4. Avoid deep nesting: Keep route hierarchies shallow (3-4 levels max)
  5. Use layouts wisely: Leverage pathless layouts (_) for shared UI
  6. Document escapes: Comment why you’re using _ escapes
  7. Consider manual config: For complex apps, manual configuration may be clearer

When to Use File-Based Routing

Good for:
  • Simple applications
  • Rapid prototyping
  • Teams familiar with file-based conventions
  • Projects with standard routing patterns
Prefer manual config when:
  • Complex routing logic
  • Routes determined at runtime
  • Heavy route code splitting
  • Team prefers explicit configuration
  • Multiple routes share the same component