blob: a2067308c6a35e554326a6a924b4616b9c84ee32 (
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
|
import productSearchApi from '@/lib/product/api/productSearchApi'
import { create } from 'xmlbuilder'
import _ from 'lodash-contrib'
export async function getServerSideProps({ res }) {
const baseUrl = process.env.SELF_HOST + '/sitemap/products'
const limit = 2500
const query = { limit }
const products = await productSearchApi({ query: _.toQuery(query) })
const pageCount = Math.ceil(products.response.numFound / limit)
const pages = Array.from({ length: pageCount }, (_, i) => i + 1)
const sitemapIndex = create('sitemapindex', { encoding: 'UTF-8' }).att(
'xmlns',
'http://www.sitemaps.org/schemas/sitemap/0.9'
)
const date = new Date()
pages.forEach((page) => {
const sitemap = sitemapIndex.ele('sitemap')
sitemap.ele('loc', `${baseUrl}/${page}.xml`)
sitemap.ele('lastmod', date.toISOString().slice(0, 10))
})
res.setHeader('Content-Type', 'text/xml')
res.write(sitemapIndex.end())
res.end()
return { props: {} }
}
export default function SitemapProducts() {
return null
}
|