Skip to main content

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 the data 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 the watch option to monitor local files for changes and trigger hot updates:
posts.data.js

Watch Patterns

The watch option accepts glob patterns:
  • watch: ['./data/*.csv'] - Watch all CSV files
  • watch: ['posts/**/*.md'] - Watch markdown files recursively
  • watch: ['data.json', 'config.yaml'] - Watch specific files

createContentLoader API

For content-focused sites, VitePress provides createContentLoader 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
Be cautious about data size when using includeSrc or render - the data is inlined as JSON in the client bundle.

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

createContentLoader implements caching based on file modified timestamps to improve dev performance. Cache is automatically invalidated when files change.
File loading uses concurrent processing controlled by buildConcurrency config option (default: CPU cores).
  • Only include necessary data in transform()
  • Avoid using render: true unless needed
  • Filter out large fields from frontmatter
  • Consider generating static files instead of inlining large datasets