-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcreate-sitemap.js
30 lines (25 loc) · 1.06 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
29
30
import { Readable } from 'stream'
import { writeFile } from 'fs/promises'
import { SitemapStream, streamToPromise } from 'sitemap'
import pages from '../src/pages.js'
const pokemonResponse = await fetch('https://pokeapi.co/api/v2/pokemon?limit=1025')
const rawPokemon = await pokemonResponse.json()
const pokemon = rawPokemon.results.map(({ name }) => name)
const dynamicMaps = {
'/pokemon/:': pokemon
}
const staticPaths = Object.values(pages)
.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)