Dynamic Routes
VitePress supports dynamic route generation, allowing you to create multiple pages from a single template with different parameters.Basic Concept
Dynamic routes use path parameters in square brackets (e.g.,[id].md) to generate multiple pages from one template.
1
Create a dynamic route file
Create
posts/[id].md:2
Create a paths loader
Create
posts/[id].paths.js or .ts:3
Generated pages
VitePress generates:
posts/hello.htmlposts/world.html
Path Loader Files
Naming Convention
For a dynamic route[param].md, create a corresponding paths file:
[param].paths.js[param].paths.ts[param].paths.mjs[param].paths.mts
defineRoutes Helper
UsedefineRoutes for type inference:
posts/[id].paths.ts
/home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:70
Path Configuration
UserRouteConfig Interface
Reference:/home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:19
Basic Paths
Async Paths
Fetch data from APIs:Watching Files
Use thewatch option to regenerate routes when files change:
posts/[id].paths.js
/home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:44
Watch Patterns
Supports glob patterns:Accessing Parameters
In Markdown
Access params via$params:
In Vue Components
UseuseData() composable:
/home/daytona/workspace/source/src/client/app/data.ts:102
Content Injection
Dynamic Content
Inject content dynamically using thecontent field:
posts/[id].paths.js
Content Marker
Use<!-- @content --> in your template:
posts/[id].md
/home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:162
Content injection is designed for CMS integration, rendering content as static local content instead of runtime data.
Page Data Transformation
Transform page data for specific routes:posts/[id].paths.ts
Multiple Parameters
Use multiple parameters in file paths:Example: [lang]/[category]/[id].md
docs/[lang]/[category]/[id].md
Paths Loader
docs/[lang]/[category]/[id].paths.js
docs/en/guide/intro.htmldocs/en/guide/advanced.htmldocs/en/api/intro.html- etc.
Advanced Example: Blog with Data
Hot Module Replacement
Dynamic routes support HMR:- Changes to
.paths.jstrigger route regeneration - Changes to watched files update affected routes
- Changes to the template update all generated pages
/home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:186
Performance
Route Caching
Route Caching
Routes are cached after first load. Cache is invalidated when:
- The paths loader file changes
- Watched files change
- Dependencies change
Concurrent Processing
Concurrent Processing
Multiple dynamic routes are resolved concurrently for faster builds.
Build Optimization
Build Optimization
.vitepress/config.js