Skip to content

Commit bb64ecb

Browse files
oliv37brc-dd
andauthored
docs: dynamic site config (#3266)
Co-authored-by: Divyansh Singh <[email protected]>
1 parent 6d89a08 commit bb64ecb

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Diff for: docs/reference/site-config.md

+56
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,62 @@ export default {
2424
}
2525
```
2626

27+
:::details Dynamic (Async) Config
28+
29+
If you need to dynamically generate the config, you can also default export a function. For example:
30+
31+
```ts
32+
import { defineConfig } from 'vitepress'
33+
34+
export default async () => defineConfig({
35+
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
36+
37+
return {
38+
// app level config options
39+
lang: 'en-US',
40+
title: 'VitePress',
41+
description: 'Vite & Vue powered static site generator.',
42+
43+
// theme level config options
44+
themeConfig: {
45+
sidebar: [
46+
...posts.map((post) => ({
47+
text: post.name,
48+
link: `/posts/${post.name}`
49+
}))
50+
]
51+
}
52+
}
53+
})
54+
```
55+
56+
You can also use top-level `await`. For example:
57+
58+
```ts
59+
import { defineConfig } from 'vitepress'
60+
61+
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
62+
63+
export default defineConfig({
64+
// app level config options
65+
lang: 'en-US',
66+
title: 'VitePress',
67+
description: 'Vite & Vue powered static site generator.',
68+
69+
// theme level config options
70+
themeConfig: {
71+
sidebar: [
72+
...posts.map((post) => ({
73+
text: post.name,
74+
link: `/posts/${post.name}`
75+
}))
76+
]
77+
}
78+
})
79+
```
80+
81+
:::
82+
2783
### Config Intellisense
2884

2985
Using the `defineConfig` helper will provide TypeScript-powered intellisense for config options. Assuming your IDE supports it, this should work in both JavaScript and TypeScript.

0 commit comments

Comments
 (0)