summaryrefslogtreecommitdiff
path: root/src/pages/sitemap/blogs.xml.js
blob: 7f41ad0fbf79c4ea5566d1544505f058faf79be8 (plain)
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
import { createSlug } from '@/core/utils/slug'
import blogsApi from '@/lib/blog/api/blogsApi'
import { create } from 'xmlbuilder'

export async function getServerSideProps({ res }) {
  const baseUrl = process.env.SELF_HOST + '/blog/'
  const blogs = await blogsApi({ limit: 0, offset: 0 })
  const sitemap = create('urlset', { encoding: 'utf-8' }).att(
    'xmlns',
    'http://www.sitemaps.org/schemas/sitemap/0.9'
  )

  const date = new Date()
  blogs.blogs.forEach((blog) => {
    const url = sitemap.ele('url')
    url.ele('loc', createSlug(baseUrl, blog.title, blog.id))
    url.ele('lastmod', date.toISOString().slice(0, 10))
    url.ele('changefreq', 'weekly')
    url.ele('priority', '0.6')
  })

  res.setHeader('Content-Type', 'text/xml')
  res.write(sitemap.end())
  res.end()

  return { props: {} }
}

export default function SitemapProducts() {
  return null
}