Build-Time Data Loading
VitePress provides a powerful data loader feature that loads arbitrary data at build time, serializes it as JSON, and makes it available to your pages and components.Overview
Data loaders enable you to:- Fetch data from remote APIs
- Generate metadata from local files
- Parse content collections (blog posts, docs, etc.)
- Build search indexes
- Create dynamic navigation
Data loading happens only at build time. The resulting data is serialized as JSON and inlined in the client bundle.
Basic Usage
A data loader file must end with.data.js or .data.ts and export a default object with a load() method.
1
Create Data Loader
2
Import in Markdown
3
Import in Components
Async Data Loading
Data loaders support async operations:Loading Local Files
When loading from local files, use thewatch option to enable hot module replacement:
How Watch Works
How Watch Works
- The
watchoption accepts glob patterns - Patterns are relative to the data loader file
- The
load()function receives absolute paths of matched files - File changes trigger hot updates in development
Since data loaders run only at build time, you can import Node.js APIs and npm packages without shipping them to the client!
createContentLoader Helper
For content-focused sites, VitePress providescreateContentLoader to simplify loading markdown files:
Basic Usage
- Usage in Component
- Data Structure
Content Loader Options
Customize what data is loaded and how it’s transformed:Custom Excerpt Separator
Control how excerpts are extracted:TypeScript Support
UsedefineLoader for type-safe data loaders:
Accessing Site Config
Access VitePress configuration inside data loaders:Implementation reference:
src/node/contentLoader.ts:80-84Using in Build Hooks
Data loaders can be used in build hooks to generate additional files:Real-World Examples
Blog Post Index
Complete Example
Complete Example
API Documentation Index
Team Members
Performance Considerations
1
Minimize Data Size
Only include necessary fields in your transform:
2
Use Excerpts Wisely
Full HTML rendering is expensive. Only enable for content that needs it:
3
Cache External Requests
Cache API responses during development:
4
Paginate Large Datasets
For large collections, implement pagination:
Troubleshooting
Data Not Updating
If changes aren’t reflected:- Ensure
watchpatterns are correct - Restart the dev server
- Check file paths are relative to the data loader
Large Bundle Size
If your bundle is too large:- Use
transformto reduce data - Disable
renderandincludeSrcif not needed - Split data into multiple loaders
- Consider dynamic imports for large datasets
Type Errors
For TypeScript issues:- Use
defineLoaderfor type inference - Declare the
dataexport explicitly - Ensure return types match declarations
See the official VitePress test suite for more examples:
__tests__/e2e/data-loading/