Setting the file. One moment.
Subchapter 12.9
references/features-dynamic-routes.mdMarkdown4 KBView on GitHub
Generate many pages from a single markdown file and dynamic data. Useful for blogs, package docs, or any data-driven pages.
Create a template file with parameter in brackets and a paths loader:
.
└─ packages/
├─ [pkg].md # Route template
└─ [pkg].paths.js # Paths loaderThe paths loader exports a paths method returning route parameters:
// packages/[pkg].paths.js
export default {
paths() {
return [
{ params: { pkg: 'foo' }},
{ params: { pkg: 'bar' }},
{ params: { pkg: 'baz' }}
]
}
}Generated pages:
/packages/foo.html/packages/bar.html/packages/baz.html.
└─ packages/
├─ [pkg]-[version].md
└─ [pkg]-[version].paths.js// packages/[pkg]-[version].paths.js
export default {
paths() {
return [
{ params: { pkg: 'foo', version: '1.0.0' }},
{ params: { pkg: 'foo', version: '2.0.0' }},
{ params: { pkg: 'bar', version: '1.0.0' }}
]
}
}From local files:
// packages/[pkg].paths.js
import fs from 'node:fs'
export default {
paths() {
return fs.readdirSync('packages').map(pkg => ({
params: { pkg }
}))
}
}From remote API:
// packages/[pkg].paths.js
export default {
async paths() {
const packages = await fetch('https://api.example.com/packages').then(r => r.json())
return packages.map(pkg => ({
params: {
pkg: pkg.name,
version: pkg.version
}
}))
}
}Template globals:
<!-- packages/[pkg].md -->
# Package: {{ $params.pkg }}
Version: {{ $params.version }}In script:
<script setup>
import { useData } from 'vitepress'
const { params } = useData()
</script>
<template>
<h1>{{ params.pkg }}</h1>
</template>For heavy content (raw markdown/HTML from CMS), use content instead of params to avoid bloating the client bundle:
// posts/[slug].paths.js
export default {
async paths() {
const posts = await fetch('https://cms.example.com/posts').then(r => r.json())
return posts.map(post => ({
params: { slug: post.slug },
content: post.content // Raw markdown or HTML
}))
}
}Render content in template:
<!-- posts/[slug].md -->
---
title: {{ $params.title }}
---
<!-- @content -->The <!-- @content --> placeholder is replaced with the content from the paths loader.
Auto-rebuild when template or data files change:
// posts/[slug].paths.js
export default {
watch: [
'./templates/**/*.njk',
'../data/**/*.json'
],
paths(watchedFiles) {
const dataFiles = watchedFiles.filter(f => f.endsWith('.json'))
return dataFiles.map(file => {
const data = JSON.parse(fs.readFileSync(file, 'utf-8'))
return {
params: { slug: data.slug },
content: renderTemplate(data)
}
})
}
}// posts/[slug].paths.js
import fs from 'node:fs'
import matter from 'gray-matter'
export default {
watch: ['./posts/*.md'],
paths(files) {
return files
.filter(f => !f.includes('[slug]'))
.map(file => {
const content = fs.readFileSync(file, 'utf-8')
const { data, content: body } = matter(content)
const slug = file.match(/([^/]+)\.md$/)[1]
return {
params: {
slug,
title: data.title,
date: data.date
},
content: body
}
})
}
}<!-- posts/[slug].md -->
---
layout: doc
---
# {{ $params.title }}
<time>{{ $params.date }}</time>
<!-- @content -->[param] syntax in filename[param].paths.js or .tspaths() returns array of { params: {...}, content?: string }$params in templates or useData().params in scriptscontent for heavy data to avoid client bundle bloatwatch enables HMR for template/data file changes