-
Notifications
You must be signed in to change notification settings - Fork 6
/
sitemap-dynamic.ts
64 lines (58 loc) · 2.03 KB
/
sitemap-dynamic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { mkdirSync, writeFileSync } from 'fs'
import { Readable } from 'stream'
import { dirname } from 'path'
import { SitemapStream, streamToPromise } from 'sitemap'
import { defineNuxtModule, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
meta: {
name: 'sitemap',
version: '0.0.1',
configKey: 'sitemap',
compatibility: { nuxt: '^3.0.0-rc.11' },
},
defaults: {
hostname: 'http://localhost:3000',
},
async setup(options, nuxt) {
async function generateSitemap(routes) {
const sitemapRoutes = routes.map((route) => route.path)
// https://github.com/ekalinin/sitemap.js#generate-a-one-time-sitemap-from-a-list-of-urls
const stream = new SitemapStream({ hostname: options.hostname })
return streamToPromise(Readable.from(sitemapRoutes).pipe(stream)).then(
(data) => data.toString()
)
}
function createSitemapFile(sitemap, filepath) {
const dirPath = dirname(filepath)
mkdirSync(dirPath, { recursive: true })
writeFileSync(filepath, sitemap)
}
const resolver = createResolver(import.meta.url)
const filePath = resolver.resolve(
nuxt.options.srcDir,
'node_modules/.cache/.sitemap/sitemap.xml'
)
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || []
nuxt.options.nitro.publicAssets.push({
baseURL: '/',
dir: dirname(filePath),
})
nuxt.hook('nitro:build:before', (nitro) => {
const paths = []
const EXCLUDED_KEYWORDS = ['/api/_content', '_payload.js', '200.html']
nitro.hooks.hook('prerender:route', (route) => {
const shouldBeAddedToSitemap = EXCLUDED_KEYWORDS.every(
(excludedKeyword) => !route.route.includes(excludedKeyword)
)
if (shouldBeAddedToSitemap) {
paths.push({ path: route.route })
}
})
nitro.hooks.hook('close', async () => {
const sitemap = await generateSitemap(paths)
createSitemapFile(sitemap, filePath)
console.log('sitemap.xml created')
})
})
},
})