Skip to content

Commit 28db15b

Browse files
committed
doc: recipe for prerender data
1 parent fc70240 commit 28db15b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/content/2.guides/5.prerendering.md

+40
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,43 @@ export default defineNuxtConfig({
6767
}
6868
})
6969
```
70+
71+
### Customizing the prerender data
72+
73+
If needed, you can customize the prerender data by using the Nitro hooks.
74+
75+
Here is a simple recipe that will extract YouTube video iframes and add them to the sitemap.
76+
77+
```ts
78+
import type { ResolvedSitemapUrl } from '#sitemap/types'
79+
80+
export default defineNuxtConfig({
81+
modules: [
82+
// run this before the sitemap moduke
83+
(_, nuxt) => {
84+
nuxt.hooks.hook('nitro:init', async (nitro) => {
85+
nitro.hooks.hook('prerender:generate', async (route) => {
86+
const html = route.contents
87+
// check for youtube video iframes and append to the videos array
88+
const matches = html.match(/<iframe.*?youtube.com\/embed\/(.*?)".*?<\/iframe>/g)
89+
if (matches) {
90+
const sitemap = route._sitemap || {} as ResolvedSitemapUrl
91+
sitemap.videos = sitemap.videos || []
92+
for (const match of matches) {
93+
const videoId = match.match(/youtube.com\/embed\/(.*?)" /)[1]
94+
sitemap.videos.push({
95+
title: 'YouTube Video',
96+
description: 'A video from YouTube',
97+
content_loc: `https://www.youtube.com/watch?v=${videoId}`,
98+
thumbnail_loc: `https://img.youtube.com/vi/${videoId}/0.jpg`,
99+
})
100+
}
101+
// the sitemap module should be able to pick this up
102+
route._sitemap = sitemap
103+
}
104+
})
105+
})
106+
},
107+
],
108+
})
109+
```

0 commit comments

Comments
 (0)