VitePress Data Loading
VitePress provides a mechanism to load data at build time and access that data in client-side components. This is very useful for fetching data from local files (such as JSON, YAML) or remote APIs and generating static content.
This functionality is implemented by creating .data.js or .data.ts files.
Basic Usage
Create Data Loader File
Create a
.data.jsfile with the same name as your Markdown file anywhere in yourdocsdirectory. For example, if you have amembers.md, you can create amembers.data.js.. └── docs ├── members.md └── members.data.jsWrite Data Loading Script
In the
.data.jsfile, you need to export aloadfunction. This function is executed during build time in the Node.js environment.javascript// docs/members.data.js import { createContentLoader } from 'vitepress' export default createContentLoader('path/to/your/data/*.md', { // ... options })createContentLoaderis a helper function for loading and transforming a set of Markdown files. The first parameter is a glob pattern used to match the files you want to load.You can also export a custom
loadfunction to fetch data from anywhere:javascript// docs/api.data.js export default { async load() { const response = await fetch('https://api.example.com/items'); const data = await response.json(); return data; // The returned data will be available on the client side } }
Accessing Data in Components
Once you've defined the data loader file, VitePress will automatically handle data loading and passing. You can directly access this data in the .md file with the same name as the data file.
Data is exposed through the useData composable function.
<!-- docs/members.md -->
<script setup>
import { useData } from 'vitepress'
// data is loaded from members.data.js
const { data: members } = useData()
</script>
# Team Members
<ul>
<li v-for="member of members">
<a :href="member.url">{{ member.frontmatter.name }}</a>
</li>
</ul>In this example, members.data.js uses createContentLoader to load a series of member Markdown files. In members.md, we retrieved this member data through useData and rendered a list using v-for.
How It Works
- Build Time (Node.js): VitePress scans your files, finds all
.data.jsfiles, and executes theloadfunction in each file, serializing the returned data into JSON. - Client Side (Browser): When a user visits a page, VitePress asynchronously loads the corresponding page's data as needed. The
useDatacomposable returns this pre-loaded data.
Caching
The result of the load function will be cached. If you want to reload data every time a file changes in development mode, you can set the watch option.
// docs/members.data.js
import { createContentLoader } from 'vitepress'
export default createContentLoader('members/*.md', {
watch: ['path/to/your/data/*.md'], // Watch for file changes
})Data loading is a powerful feature of VitePress that allows you to combine static content with dynamic data sources to create more complex, data-driven websites while maintaining the performance advantages of static sites.