mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 23:16:47 +00:00
feat: add auto-generated sitemap
This commit is contained in:
36
pages/sitemap.xml.tsx
Normal file
36
pages/sitemap.xml.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import React from 'react'
|
||||
|
||||
import { SiteMap } from 'lib/types'
|
||||
import { host } from 'lib/config'
|
||||
import { getSiteMaps } from 'lib/get-site-maps'
|
||||
|
||||
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>
|
||||
<loc>${host}/</loc>
|
||||
</url>
|
||||
|
||||
${Object.keys(siteMap.canonicalPageMap)
|
||||
.map((canonicalPagePath) =>
|
||||
`
|
||||
<url>
|
||||
<loc>${host}/${canonicalPagePath}</loc>
|
||||
</url>
|
||||
`.trim()
|
||||
)
|
||||
.join('')}
|
||||
</urlset>
|
||||
`
|
||||
|
||||
export default class Sitemap extends React.Component {
|
||||
static async getInitialProps({ res }) {
|
||||
const siteMaps = await getSiteMaps()
|
||||
|
||||
res.setHeader('Content-Type', 'text/xml')
|
||||
res.write(createSitemap(siteMaps[0]))
|
||||
res.end()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user