forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsitemap.xml.tsx
57 lines (48 loc) · 1.24 KB
/
sitemap.xml.tsx
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { GetServerSideProps } from 'next'
import { host } from '@/lib/config'
import { getSiteMap } from '@/lib/get-site-map'
import type { SiteMap } from '@/lib/types'
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 () => null