Files
nextjs-notion-starter-kit/pages/sitemap.xml.tsx
2024-10-31 20:49:33 -05:00

60 lines
1.3 KiB
TypeScript

import type { GetServerSideProps } from 'next'
import type { SiteMap } from '@/lib/types'
import { host } from '@/lib/config'
import { getSiteMap } from '@/lib/get-site-map'
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
if (req.method !== 'GET') {
res.statusCode = 405
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify({ error: 'method not allowed' }))
res.end()
return {
props: {}
}
}
const siteMap = await getSiteMap()
// cache for up to 8 hours
res.setHeader(
'Cache-Control',
'public, max-age=28800, stale-while-revalidate=28800'
)
res.setHeader('Content-Type', 'text/xml')
res.write(createSitemap(siteMap))
res.end()
return {
props: {}
}
}
const createSitemap = (siteMap: SiteMap) =>
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${host}</loc>
</url>
<url>
<loc>${host}/</loc>
</url>
${Object.keys(siteMap.canonicalPageMap)
.map((canonicalPagePath) =>
`
<url>
<loc>${host}/${canonicalPagePath}</loc>
</url>
`.trim()
)
.join('')}
</urlset>
`
export default function noop() {
return null
}