Merge pull request #170 from JaeSeoKim/main

This commit is contained in:
Travis Fischer
2022-03-27 06:31:08 -04:00
committed by GitHub
3 changed files with 90 additions and 8 deletions

View File

@@ -8,14 +8,6 @@ export const getStaticProps = async (context) => {
const rawPageId = context.params.pageId as string
try {
if (rawPageId === 'sitemap.xml' || rawPageId === 'robots.txt') {
return {
redirect: {
destination: `/api/${rawPageId}`
}
}
}
const props = await resolveNotionPage(domain, rawPageId)
return { props, revalidate: 10 }

31
pages/robots.txt.tsx Normal file
View File

@@ -0,0 +1,31 @@
import { GetServerSideProps } from 'next'
import { host } from 'lib/config'
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: {}
}
}
res.setHeader(
'Cache-Control',
'public, s-maxage=60, max-age=60, stale-while-revalidate=60'
)
res.setHeader('Content-Type', 'text/plain')
res.write(`User-agent: *
Sitemap: ${host}/sitemap.xml
`)
res.end()
return {
props: {}
}
}
const RobotsTxt: React.FC = () => null
export default RobotsTxt

59
pages/sitemap.xml.tsx Normal file
View File

@@ -0,0 +1,59 @@
import { GetServerSideProps } from 'next'
import { SiteMap } from 'lib/types'
import { host } from 'lib/config'
import { getSiteMaps } from 'lib/get-site-maps'
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 siteMaps = await getSiteMaps()
// cache sitemap for up to one hour
res.setHeader(
'Cache-Control',
'public, s-maxage=3600, max-age=3600, stale-while-revalidate=3600'
)
res.setHeader('Content-Type', 'text/xml')
res.write(createSitemap(siteMaps[0]))
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>
`
const SiteMapXml: React.FC = () => null
export default SiteMapXml