manifold/web/pages/server-sitemap.xml.tsx

28 lines
1021 B
TypeScript
Raw Normal View History

import _ from 'lodash'
2022-03-24 16:52:13 +00:00
import { GetServerSideProps } from 'next'
import { getServerSideSitemap, ISitemapField } from 'next-sitemap'
import { DOMAIN } from 'common/envs/constants'
import { LiteMarket } from './api/v0/_types'
2022-03-24 16:52:13 +00:00
export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Fetching data from https://manifold.markets/api
2022-03-24 16:52:13 +00:00
const response = await fetch(`https://${DOMAIN}/api/v0/markets`)
const liteMarkets = (await response.json()) as LiteMarket[]
const sortedMarkets = _.sortBy(liteMarkets, (m) => -m.volume24Hours)
const fields = sortedMarkets.map((market) => ({
2022-03-24 16:52:13 +00:00
// See https://www.sitemaps.org/protocol.html
loc: market.url,
changefreq: market.volume24Hours > 10 ? 'hourly' : 'daily',
priority: market.volume24Hours + market.volume7Days > 100 ? 0.7 : 0.1,
2022-03-24 16:52:13 +00:00
// TODO: Add `lastmod` aka last modified time
})) as ISitemapField[]
return await getServerSideSitemap(ctx, fields)
2022-03-24 16:52:13 +00:00
}
// Default export to prevent next.js errors
export default function Sitemap() {}