Skip to main content

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

The data loader itself does not export data. VitePress calls the load() method behind the scenes and implicitly exposes the result via the data named export.

Async Data Loading

Data loaders support async operations:

Loading Local Files

When loading from local files, use the watch option to enable hot module replacement:
  • The watch option 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 provides createContentLoader to simplify loading markdown files:

Basic Usage

Content Loader Options

Customize what data is loaded and how it’s transformed:
Be cautious about data size. The loaded data is inlined as JSON in the client bundle. Use transform to filter and reduce data before serialization.

Custom Excerpt Separator

Control how excerpts are extracted:

TypeScript Support

Use defineLoader for type-safe data loaders:
Now imports are fully typed:

Accessing Site Config

Access VitePress configuration inside data loaders:
Implementation reference: src/node/contentLoader.ts:80-84

Using in Build Hooks

Data loaders can be used in build hooks to generate additional files:

Real-World Examples

Blog Post Index

Use in a page:

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:
  1. Ensure watch patterns are correct
  2. Restart the dev server
  3. Check file paths are relative to the data loader

Large Bundle Size

If your bundle is too large:
  1. Use transform to reduce data
  2. Disable render and includeSrc if not needed
  3. Split data into multiple loaders
  4. Consider dynamic imports for large datasets

Type Errors

For TypeScript issues:
  1. Use defineLoader for type inference
  2. Declare the data export explicitly
  3. Ensure return types match declarations
See the official VitePress test suite for more examples: __tests__/e2e/data-loading/