/
create-sitemap.js
28 lines (23 loc) · 1.05 KB
/
create-sitemap.js
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
import { Readable } from 'stream'
import { writeFile } from 'fs/promises'
import axios from 'axios'
import { SitemapStream, streamToPromise } from 'sitemap'
import pagesManifest from '../src/pages-manifest.js'
const rawPokemon = await axios('https://pokeapi.co/api/v2/pokemon?limit=10000')
const pokemon = rawPokemon.data.results.map(({ name }) => name)
const dynamicMaps = {
'/pokemon/:': pokemon
}
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 links = paths.map(path => ({ url: path, changefreq: 'weekly' }))
const stream = new SitemapStream({ hostname: 'https://client-side-rendering.pages.dev' })
streamToPromise(Readable.from(links).pipe(stream))
.then(data => data.toString())
.then(res => writeFile('public/sitemap.xml', res))
.then(() => console.log('Sitemap created.'))
.catch(console.log)