Skip to main content

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.html
  • posts/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

Use defineRoutes for type inference:
posts/[id].paths.ts
Reference: /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 the watch option to regenerate routes when files change:
posts/[id].paths.js
Reference: /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

Use useData() composable:
Reference: /home/daytona/workspace/source/src/client/app/data.ts:102

Content Injection

Dynamic Content

Inject content dynamically using the content field:
posts/[id].paths.js

Content Marker

Use <!-- @content --> in your template:
posts/[id].md
Reference: /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
Generates:
  • docs/en/guide/intro.html
  • docs/en/guide/advanced.html
  • docs/en/api/intro.html
  • etc.

Advanced Example: Blog with Data

Hot Module Replacement

Dynamic routes support HMR:
  • Changes to .paths.js trigger route regeneration
  • Changes to watched files update affected routes
  • Changes to the template update all generated pages
Reference: /home/daytona/workspace/source/src/node/plugins/dynamicRoutesPlugin.ts:186

Performance

Routes are cached after first load. Cache is invalidated when:
  • The paths loader file changes
  • Watched files change
  • Dependencies change
Multiple dynamic routes are resolved concurrently for faster builds.
.vitepress/config.js

Troubleshooting

If you see “Missing paths file for dynamic route” warnings, ensure:
  • The .paths.js file exists alongside the .md file
  • The file names match exactly (e.g., [id].md[id].paths.js)
  • The file exports a default object with a paths property