Skip to content

Commit 6b16dad

Browse files
userquinbrc-dd
andauthored
feat: add transformHead hook (vuejs#1323)
Co-authored-by: Divyansh Singh <[email protected]>
1 parent 8e4ff4d commit 6b16dad

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

docs/config/app-configs.md

+26-15
Original file line numberDiff line numberDiff line change
@@ -255,24 +255,21 @@ VitePress build hooks allow you to add new functionality and behaviors to your w
255255
- Search Indexing
256256
- PWA
257257

258-
### transformHtml
258+
### transformHead
259259

260-
- Type: `( code: string, id: string, ctx: TransformContext ) => Awaitable<string | void>`
260+
- Type: `(ctx: TransformContext) => Awaitable<HeadConfig[]>`
261261

262-
`transformHtml` is a build hook to transform the content of each page before saving to disk (SSG).
262+
`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.
263263

264264
::: warning
265-
Modifying the html content may cause hydration problems in runtime.
265+
Don't mutate anything inside the `ctx`.
266266
:::
267267

268268
```ts
269-
import { defineConfig } from 'vitepress'
270-
271-
export default defineConfig({
272-
/* other vitepress options */
273-
async transformHtml(code, id, context) {
269+
export default {
270+
async transformHead(ctx) {
274271
}
275-
})
272+
}
276273
```
277274

278275
```ts
@@ -287,18 +284,32 @@ interface TransformContext {
287284
}
288285
```
289286

287+
### transformHtml
288+
289+
- Type: `(code: string, id: string, ctx: TransformContext) => Awaitable<string | void>`
290+
291+
`transformHtml` is a build hook to transform the content of each page before saving to disk.
292+
293+
::: warning
294+
Don't mutate anything inside the `ctx`. Also, modifying the html content may cause hydration problems in runtime.
295+
:::
296+
297+
```ts
298+
export default {
299+
async transformHtml(code, id, context) {
300+
}
301+
}
302+
```
303+
290304
### buildEnd
291305

292306
- Type: `(siteConfig: SiteConfig) => Awaitable<void>`
293307

294308
`buildEnd` is a build CLI hook, it will run after build (SSG) finish but before VitePress CLI process exits.
295309

296310
```ts
297-
import { defineConfig } from 'vitepress'
298-
299-
export default defineConfig({
300-
/* other vitepress options */
311+
export default {
301312
async buildEnd(siteConfig) {
302313
}
303-
})
314+
}
304315
```

src/node/build/render.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,24 @@ export async function renderPage(
106106
const title: string = createTitle(siteData, pageData)
107107
const description: string = pageData.description || siteData.description
108108

109-
const head = mergeHead(
109+
const headBeforeTransform = mergeHead(
110110
siteData.head,
111111
filterOutHeadDescription(pageData.frontmatter.head)
112112
)
113113

114+
const head = mergeHead(
115+
headBeforeTransform,
116+
(await config.transformHead?.({
117+
siteConfig: config,
118+
siteData,
119+
pageData,
120+
title,
121+
description,
122+
head: headBeforeTransform,
123+
content
124+
})) || []
125+
)
126+
114127
let inlinedScript = ''
115128
if (config.mpa && result) {
116129
const matchingChunk = result.output.find(

src/node/config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ export interface UserConfig<ThemeConfig = any> {
9393
*/
9494
buildEnd?: (siteConfig: SiteConfig) => Awaitable<void>
9595

96+
/**
97+
* Head transform hook: runs before writing HTML to dist.
98+
*
99+
* This build hook will allow you to modify the head adding new entries that cannot be statically added.
100+
*/
101+
transformHead?: (ctx: TransformContext) => Awaitable<HeadConfig[]>
102+
96103
/**
97104
* HTML transform hook: runs before writing HTML to dist.
98105
*/
@@ -129,6 +136,7 @@ export interface SiteConfig<ThemeConfig = any>
129136
| 'ignoreDeadLinks'
130137
| 'cleanUrls'
131138
| 'buildEnd'
139+
| 'transformHead'
132140
| 'transformHtml'
133141
> {
134142
root: string
@@ -215,6 +223,7 @@ export async function resolveConfig(
215223
ignoreDeadLinks: userConfig.ignoreDeadLinks,
216224
cleanUrls: userConfig.cleanUrls || 'disabled',
217225
buildEnd: userConfig.buildEnd,
226+
transformHead: userConfig.transformHead,
218227
transformHtml: userConfig.transformHtml
219228
}
220229

0 commit comments

Comments
 (0)