summaryrefslogtreecommitdiff
path: root/src/pages/sitemap/categories.xml.js
blob: 1c684576e5ba73fba5fc3ce4fa59178933f37d4a (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
32
33
34
35
36
37
38
39
40
41
import { create } from 'xmlbuilder'
import { createSlug } from '@/core/utils/slug'
import odooApi from '@/core/api/odooApi'

export async function getServerSideProps({ res }) {
  const categories = await odooApi('GET', '/api/v1/category/tree')
  const sitemap = create('urlset', { encoding: 'utf-8' }).att(
    'xmlns',
    'http://www.sitemaps.org/schemas/sitemap/0.9'
  )

  categories.forEach((category) => {
    addUrlToSitemap(sitemap, category.name, category.id)
    category.childs.forEach((child1Category) => {
      addUrlToSitemap(sitemap, child1Category.name, child1Category.id)
      child1Category.childs.forEach((child2Category) => {
        addUrlToSitemap(sitemap, child2Category.name, child2Category.id)
      })
    })
  })

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

  return { props: {} }
}

function addUrlToSitemap(sitemap, name, id) {
  const baseUrl = process.env.SELF_HOST + '/shop/category/'
  const date = new Date()
  const url = sitemap.ele('url')
  url.ele('loc', createSlug(baseUrl, name, id))
  url.ele('lastmod', date.toISOString().slice(0, 10))
  url.ele('changefreq', 'weekly')
  url.ele('priority', '0.6')
}

export default function SitemapProducts() {
  return null
}