Data Loading

Build-time data loaders for fetching remote data or processing local files

VitePress data loaders run at build time to load arbitrary data that's serialized as JSON in the client bundle.

Basic Usage

Create a file ending with .data.js or .data.ts:

// example.data.ts
export default {
  load() {
    return {
      hello: 'world',
      timestamp: Date.now()
    }
  }
}

Import the data named export:

<script setup>
import { data } from './example.data.ts'
</script>

<template>
  <pre>{{ data }}</pre>
</template>

Async Data

Fetch remote data:

Local Files with Watch

Process local files with hot reload:

createContentLoader

Helper for loading markdown content (common for blogs/archives):

Returns array of ContentData:

With options:

Usage Example: Blog Index

Typed Data Loaders

In Build Hooks

Use in config for generating additional files:

Accessing Config

Key Points

  • Data loaders run only at build time in Node.js

  • File must end with .data.js or .data.ts

  • Import the data named export (not default)

  • Use watch for local file hot reload during dev

  • createContentLoader simplifies loading markdown collections

  • Keep data small - it's inlined in the client bundle

  • Heavy data should use transform to reduce payload

Last updated

Was this helpful?