Skip to main content

Routing

VitePress uses file-based routing, which means your Markdown files automatically become pages based on their location in your project. No routing configuration needed!

File-Based Routing Basics

Every .md file in your source directory becomes an HTML page with the same path structure.

Simple Example

Given this file structure:
VitePress generates these URLs:
The resulting HTML can be hosted on any web server that serves static files.

Project Root vs Source Directory

Understanding these concepts is key to organizing your VitePress project.

Project Root

The project root is where VitePress looks for the .vitepress directory:
Run VitePress commands from your project directory:
This produces:

Source Directory

The source directory is where your Markdown files live. By default, it’s the same as the project root, but you can customize it:
.vitepress/config.js
With this configuration:
Your routes become:
The srcDir option is useful for keeping your documentation separate from configuration files.

Linking Between Pages

Use both absolute and relative paths in your Markdown links.

Best Practices

Linking to Non-VitePress Pages

For pages not generated by VitePress, specify the target explicitly:
Or use HTML anchor tags:

Clean URLs

Remove .html extensions from your URLs for cleaner, more professional links.
Clean URLs require server-side support. Not all hosting platforms support this feature.

How It Works

By default: example.com/path.html
With clean URLs: example.com/path

Enable Clean URLs

1

Check server support

These platforms support clean URLs automatically:
2

Enable in VitePress config

Add to .vitepress/config.js:
.vitepress/config.js
With clean URLs enabled:
  • Internal links are generated without .html extensions
  • Client-side redirects from .html to clean URLs
  • Better SEO and user experience

Manual Clean URLs

If your server doesn’t support clean URLs, use this directory structure:

Route Rewrites

Customize the mapping between source files and generated URLs - perfect for monorepos and complex structures.

Use Case: Monorepo Documentation

You have packages with documentation alongside source code:
You want these URLs:

Static Rewrites

Define explicit mappings:
.vitepress/config.js

Dynamic Rewrites with Parameters

Use route parameters to avoid repetition:
.vitepress/config.js
Rewrite patterns use the path-to-regexp package syntax.

Function-Based Rewrites

Use a function for complex rewrite logic:
.vitepress/config.js
Important: When using rewrites, relative links should be based on the rewritten paths, not the original file structure.

Dynamic Routes

Generate multiple pages from a single template using dynamic data - perfect for API documentation, blog posts, or product catalogs.

The Basics

Create a route template with parameters in square brackets:
The [pkg] parameter will be replaced with actual values from your paths loader.

Paths Loader File

Create a .paths.js file that exports a paths function:
packages/[pkg].paths.js
This generates:

TypeScript Support

Use defineRoutes for type safety:
packages/[pkg].paths.ts
The defineRoutes helper provides TypeScript intellisense for route configuration.

Multiple Parameters

Use multiple parameters in your route:

Generating Paths from Data

Load data from local files or remote APIs:

Watching Data Files

Automatically rebuild pages when data or templates change:
posts/[slug].paths.js
The watch option accepts glob patterns relative to the .paths.js file.

Accessing Parameters in Pages

Use parameters in your Markdown template:
packages/[pkg].md
Or in Vue components:

Rendering Raw Content

Inject content from external sources without passing it as parameters:
In your template, use the special @content marker:
Avoid passing large content in params as it’s serialized in the client JavaScript bundle. Use the content property instead.

Setting a Base Path

If your site is deployed to a subdirectory, configure the base path:
.vitepress/config.js
GitHub/GitLab Pages: Set base to '/repo/' if deploying to user.github.io/repo/

Next Steps

Deploy Your Site

Learn how to build and deploy your VitePress site

Markdown Features

Explore VitePress’s powerful Markdown extensions

Asset Handling

Learn how to work with images and static files

Data Loading

Load data from local or remote sources