Build-Time Data Loading
VitePress provides powerful data loaders that execute at build time, allowing you to load arbitrary data and import it from pages or components. The loaded data is serialized as JSON in the final JavaScript bundle.Basic Data Loaders
Data loader files must end with.data.js or .data.ts and export a default object with a load() method.
Importing Data
Import data from loader files using thedata named export:
The loader module is evaluated only in Node.js, so you can import Node APIs and npm dependencies freely.
Loading Local Files
Use thewatch option to monitor local files for changes and trigger hot updates:
posts.data.js
Watch Patterns
Thewatch option accepts glob patterns:
watch: ['./data/*.csv']- Watch all CSV fileswatch: ['posts/**/*.md']- Watch markdown files recursivelywatch: ['data.json', 'config.yaml']- Watch specific files
createContentLoader API
For content-focused sites, VitePress providescreateContentLoader to simplify loading markdown files:
1
Create a data loader
posts.data.js
2
Import and use the data
ContentData Interface
The loaded data has the following structure:Transform Options
posts.data.js
Using in Build Hooks
Data loaders can be used in build hooks to generate files:.vitepress/config.js
Accessing Configuration
Access VitePress configuration inside loaders:Advanced Example: API Index
Generate an API index from markdown files:api.data.ts
Performance Considerations
Caching
Caching
createContentLoader implements caching based on file modified timestamps to improve dev performance. Cache is automatically invalidated when files change.Concurrency
Concurrency
File loading uses concurrent processing controlled by
buildConcurrency config option (default: CPU cores).Bundle Size
Bundle Size
- Only include necessary data in
transform() - Avoid using
render: trueunless needed - Filter out large fields from frontmatter
- Consider generating static files instead of inlining large datasets