AgentSkillsCN

react-router-file-routes

借助强大的语法支持(带引号的标签、ELK 布局),生成精美且手绘的 Mermaid 图表。当用户提出“绘制图表”、“制作流程图”、“生成序列图”或“可视化这一流程”时,可选用此技能。

SKILL.md
--- frontmatter
name: react-router-file-routes
description: Reference guide for React Router v7 file-based routing conventions. Use when creating or organizing routes in React Router v7 projects, naming route files, or working with dynamic segments, nested routes, pathless layouts, optional segments, splat routes, and special characters in file-based routing.

React Router File Routes

Complete reference for React Router v7 file-based routing conventions and file naming patterns.

Core Concepts

React Router v7 uses file-based routing where file names in app/routes/ directory automatically become routes. Special characters in file names create specific routing behaviors.

File Naming Conventions

Basic Routes

File NameURL PathDescription
_index.tsx/Index route at the root or parent path
about.tsx/aboutSimple static route
contact.tsx/contactAnother static route

Nested Routes with Dots

Dots (.) create nested URL paths and establish parent-child relationships.

File NameURL PathParent Layout
concerts.tsx/concertsroot.tsx
concerts._index.tsx/concertsconcerts.tsx
concerts.trending.tsx/concerts/trendingconcerts.tsx
concerts.$city.tsx/concerts/:cityconcerts.tsx
settings.profile.tsx/settings/profileroot.tsx
settings.profile.edit.tsx/settings/profile/editroot.tsx

Example structure:

code
app/
├── routes/
│   ├── _index.tsx
│   ├── concerts.tsx              # Layout for /concerts/*
│   ├── concerts._index.tsx       # Renders at /concerts
│   ├── concerts.trending.tsx     # Renders at /concerts/trending
│   └── concerts.$city.tsx        # Renders at /concerts/:city
└── root.tsx

Dynamic Segments

Prefix with $ to create URL parameters.

File NameURL PathParams
blog.$slug.tsx/blog/:slug{ slug: string }
users.$userId.tsx/users/:userId{ userId: string }
teams.$teamId.projects.$projectId.tsx/teams/:teamId/projects/:projectId{ teamId: string, projectId: string }

Access params in component:

typescript
import type { Route } from "./+types/blog.$slug";

export async function loader({ params }: Route.LoaderArgs) {
  // params.slug is available here
}

export default function Component({ params }: Route.ComponentProps) {
  return <div>Post: {params.slug}</div>;
}

Optional Segments

Append ? to make a segment optional (works with both static and dynamic segments).

File NameURL PathMatches
$lang.categories.tsx/:lang?/categories/categories and /en/categories
users.$userId.edit.tsx/users/:userId/edit?/users/123 and /users/123/edit

Pathless Layout Routes

Prefix with _ (underscore) to create layouts that don't affect the URL structure.

File NameURL PathLayout Parent
_auth.tsxN/A (pathless)root.tsx
_auth.login.tsx/login_auth.tsx
_auth.register.tsx/register_auth.tsx

Example structure:

code
app/
├── routes/
│   ├── _auth.tsx                 # Pathless layout
│   ├── _auth.login.tsx          # URL: /login (wrapped in _auth layout)
│   ├── _auth.register.tsx       # URL: /register (wrapped in _auth layout)
│   └── dashboard.tsx            # URL: /dashboard (not wrapped in _auth)
└── root.tsx

Escaping Layouts with Trailing Underscore

Suffix with _ to create a nested URL without inheriting parent layout.

File NameURL PathLayout Parent
concerts.tsx/concertsroot.tsx
concerts.trending.tsx/concerts/trendingconcerts.tsx
concerts_.mine.tsx/concerts/mineroot.tsx (skips concerts.tsx)

Splat Routes (Catch-All)

Use $ to create catch-all routes that match remaining URL segments.

File NameURL PathParams
files.$.tsx/files/*{ "*": string }
$.tsx/*{ "*": string } (404 catch-all)

Access splat params:

typescript
export async function loader({ params }: Route.LoaderArgs) {
  const filePath = params["*"]; // Everything after /files/
  return getFileInfo(filePath);
}

Escaping Special Characters

Use [] brackets to escape special characters and include them literally in URLs.

File NameURL PathPurpose
sitemap[.]xml.tsx/sitemap.xmlResource route with extension
blog[$slug].tsx/blog/$slugLiteral $slug in URL (not a param)
about[.].tsx/about.Literal dot in URL

Complete File Structure Examples

Standard Nested Routes

code
app/
├── routes/
│   ├── _index.tsx           # /
│   ├── about.tsx            # /about
│   ├── concerts.tsx         # /concerts layout
│   ├── concerts._index.tsx  # /concerts
│   ├── concerts.$city.tsx   # /concerts/:city
│   └── concerts.trending.tsx # /concerts/trending
└── root.tsx

Pathless Layout Routes

code
app/
├── routes/
│   ├── _auth.tsx            # Pathless layout
│   ├── _auth.login.tsx      # /login
│   ├── _auth.register.tsx   # /register
│   └── dashboard.tsx        # /dashboard
└── root.tsx

Mixed Layouts

code
app/
├── routes/
│   ├── _index.tsx              # /
│   ├── concerts.tsx            # /concerts layout
│   ├── concerts._index.tsx     # /concerts
│   ├── concerts.trending.tsx   # /concerts/trending
│   └── concerts_.mine.tsx      # /concerts/mine (skips layout)
└── root.tsx

Quick Reference

PatternFile ExampleURL ResultNotes
Simple routeabout.tsx/aboutStatic route
Nested patha.b.c.tsx/a/b/cDots create slashes
Index routeconcerts._index.tsx/concertsDefault child route
Dynamic segmentblog.$slug.tsx/blog/:slugCreates param
Multiple params$a.$b.tsx/:a/:bMultiple dynamic segments
Optional segment$lang?.about.tsx/about or /:lang/aboutOptional param
Pathless layout_auth.login.tsx/loginUses _auth.tsx layout
Escape layoutparent_.child.tsx/parent/childSkips parent.tsx layout
Splat routefiles.$.tsx/files/*Catches all after /files/
Escape charsapi[.]json.tsx/api.jsonLiteral . in URL

Common Patterns

Authentication Routes

code
_auth.tsx              # Shared auth layout
_auth.login.tsx       # /login
_auth.register.tsx    # /register
_auth.forgot.tsx      # /forgot

Admin Dashboard

code
_admin.tsx                    # Admin layout
_admin._index.tsx            # /admin
_admin.users.tsx             # /admin/users
_admin.users.$userId.tsx     # /admin/users/:userId

Blog with Categories

code
blog.tsx                     # Blog layout
blog._index.tsx             # /blog
blog.$slug.tsx              # /blog/:slug
blog.category.$cat.tsx      # /blog/category/:cat

Resource Routes

code
api.json.tsx                # /api/json
sitemap[.]xml.tsx          # /sitemap.xml
robots[.]txt.tsx           # /robots.txt

scripts/

Executable code (Python/Bash/etc.) that can be run directly to perform specific operations.

Examples from other skills:

  • PDF skill: fill_fillable_fields.py, extract_form_field_info.py - utilities for PDF manipulation
  • DOCX skill: document.py, utilities.py - Python modules for document processing

Appropriate for: Python scripts, shell scripts, or any executable code that performs automation, data processing, or specific operations.

Note: Scripts may be executed without loading into context, but can still be read by Codex for patching or environment adjustments.

references/

Documentation and reference material intended to be loaded into context to inform Codex's process and thinking.

Examples from other skills:

  • Product management: communication.md, context_building.md - detailed workflow guides
  • BigQuery: API reference documentation and query examples
  • Finance: Schema documentation, company policies

Appropriate for: In-depth documentation, API references, database schemas, comprehensive guides, or any detailed information that Codex should reference while working.

assets/

Files not intended to be loaded into context, but rather used within the output Codex produces.

Examples from other skills:

  • Brand styling: PowerPoint template files (.pptx), logo files
  • Frontend builder: HTML/React boilerplate project directories
  • Typography: Font files (.ttf, .woff2)

Appropriate for: Templates, boilerplate code, document templates, images, icons, fonts, or any files meant to be copied or used in the final output.


Not every skill requires all three types of resources.