MathiasKandelborg/TRUE-framework

View on GitHub
pages/api/sitemap.ts

Summary

Maintainability
A
0 mins
Test Coverage
import createRoutesForSitemap from '@util/createRoutesForSitemap'
import { NextApiRequest, NextApiResponse } from 'next'
import { SitemapStream, streamToPromise } from 'sitemap'

/**
 * @param {NextApiRequest} req Incomming request
 * @param {NextApiResponse} res Response
 * @returns {void}
 */
export default async function sitemapFunc(
  req: NextApiRequest,
  res: NextApiResponse
): Promise<void> {
  res.setHeader('Content-Type', 'text/xml')
  try {
    const createdRoutesForSitemap = await createRoutesForSitemap() // call the backend and fetch all stories

    const smStream = new SitemapStream({
      hostname: `https://${req?.headers?.host || 'not found'}`
    })

    createdRoutesForSitemap.map((routesArray) =>
      Object.keys(routesArray).map((routeName) => {
        const route = routesArray[routeName]

        return smStream.write({
          url: routeName,
          lastmod: route._updatedAt
        })
      })
    )

    smStream.end()

    const sitemap = await streamToPromise(smStream).then((sm) => sm.toString())

    res.write(sitemap)
    res.end()
  } catch (e) {
    console.error(e)
    res.statusCode = 500
    res.end()
  }
}