Skip to content

Commit fb93460

Browse files
committed
docs: update docs on transform page data
1 parent 9a062a6 commit fb93460

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

docs/reference/site-config.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ interface SSGContext {
610610
`transformHead` is a build hook to transform the head before generating each page. It will allow you to add head entries that cannot be statically added to your VitePress config. You only need to return extra entries, they will be merged automatically with the existing ones.
611611

612612
::: warning
613-
Don't mutate anything inside the `ctx`.
613+
Don't mutate anything inside the `context`.
614614
:::
615615

616616
```ts
@@ -635,14 +635,34 @@ interface TransformContext {
635635
}
636636
```
637637

638+
Note that this hook is only called when generating the site statically. It is not called during dev. If you need to add dynamic head entries during dev, you can use the [`transformPageData`](#transformpagedata) hook instead:
639+
640+
```ts
641+
export default {
642+
transformPageData(pageData) {
643+
pageData.frontmatter.head ??= []
644+
pageData.frontmatter.head.push([
645+
'meta',
646+
{
647+
name: 'og:title',
648+
content:
649+
pageData.frontmatter.layout === 'home'
650+
? `VitePress`
651+
: `${pageData.title} | VitePress`
652+
}
653+
])
654+
}
655+
}
656+
```
657+
638658
### transformHtml
639659

640-
- Type: `(code: string, id: string, ctx: TransformContext) => Awaitable<string | void>`
660+
- Type: `(code: string, id: string, context: TransformContext) => Awaitable<string | void>`
641661

642662
`transformHtml` is a build hook to transform the content of each page before saving to disk.
643663

644664
::: warning
645-
Don't mutate anything inside the `ctx`. Also, modifying the html content may cause hydration problems in runtime.
665+
Don't mutate anything inside the `context`. Also, modifying the html content may cause hydration problems in runtime.
646666
:::
647667

648668
```ts
@@ -655,12 +675,12 @@ export default {
655675

656676
### transformPageData
657677

658-
- Type: `(pageData: PageData, ctx: TransformPageContext) => Awaitable<Partial<PageData> | { [key: string]: any } | void>`
678+
- Type: `(pageData: PageData, context: TransformPageContext) => Awaitable<Partial<PageData> | { [key: string]: any } | void>`
659679

660-
`transformPageData` is a hook to transform the `pageData` of each page. You can directly mutate `pageData` or return changed values which will be merged into PageData.
680+
`transformPageData` is a hook to transform the `pageData` of each page. You can directly mutate `pageData` or return changed values which will be merged into the page data.
661681

662682
::: warning
663-
Don't mutate anything inside the `ctx`.
683+
Don't mutate anything inside the `context` and be careful that this might impact the performance of dev server, especially if you have some network requests or heavy computations (like generating images) in the hook. You can check for `process.env.NODE_ENV === 'production'` for conditional logic.
664684
:::
665685

666686
```ts

0 commit comments

Comments
 (0)