-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap-builder.js
37 lines (31 loc) · 1.06 KB
/
sitemap-builder.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
31
32
33
34
35
36
37
const { SitemapStream, streamToPromise } = require('sitemap')
const { createWriteStream } = require('fs')
const axios = require('axios')
async function generateSitemap() {
try {
const data = await axios.get(
`https://ali-website-server.onrender.com/api/blogs?fields[0]=id`
)
const links = [
{ url: '/', changefreq: 'weekly', priority: 0.8 },
{ url: '/blog', changefreq: 'weekly', priority: 0.8 },
{ url: '/about', changefreq: 'monthly', priority: 0.7 },
{ url: '/contact', changefreq: 'monthly', priority: 0.7 },
...data.data.data.map((post) => ({
url: `/blog/${post.id}`,
changefreq: 'weekly',
priority: 0.9,
})),
]
const sitemapStream = new SitemapStream({
hostname: 'https://alialsyoufchemist.com/',
})
sitemapStream.pipe(createWriteStream('./public/sitemap.xml'))
links.forEach((link) => sitemapStream.write(link))
sitemapStream.end()
console.log('Sitemap generated!')
} catch (err) {
console.log('Failed to generate a sitemap: ', err)
}
}
generateSitemap()