mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
60 lines
1.3 KiB
TypeScript
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
|
|
}
|