-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcreate-sitemap.mjs
25 lines (20 loc) · 1000 Bytes
/
create-sitemap.mjs
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
import { Readable } from 'stream'
import { writeFile } from 'fs/promises'
import { SitemapStream, streamToPromise } from 'sitemap'
import pagesManifest from '../src/pages-manifest.json' assert { type: 'json' }
const dynamicMaps = {
'/pokemon/:': ['pichu', 'pikachu', 'raichu']
}
const staticPaths = pagesManifest.filter(({ path }) => !path.includes(':')).map(({ path }) => path)
const dynamicPaths = Object.keys(dynamicMaps).reduce(
(acc, path) => [...acc, ...dynamicMaps[path].map(value => path.replace(':', value))],
[]
)
const paths = [...staticPaths, ...dynamicPaths]
const stream = new SitemapStream({ hostname: 'https://client-side-rendering.pages.dev' })
const links = paths.map(path => ({ url: path, changefreq: 'daily' }))
streamToPromise(Readable.from(links).pipe(stream))
.then(data => data.toString())
.then(res => writeFile('public/sitemap.xml', res))
.then(() => console.log(`Sitemap created with the following paths: \n\n${paths.join('\n')}\n`))
.catch(console.log)