feat: Change sitemap.xml, robots.xml end-point

This commit is contained in:
JaeSeoKim
2021-10-17 00:44:46 +09:00
parent 1d19cd0d6c
commit d1e495e000
4 changed files with 48 additions and 41 deletions

View File

@@ -1,23 +0,0 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { host } from '../lib/config'
export default async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> => {
if (req.method !== 'GET') {
return res.status(405).send({ error: 'method not allowed' })
}
// cache robots.txt for up to 60 seconds
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}/api/sitemap.xml
`)
res.end()
}

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 }

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

@@ -0,0 +1,30 @@
import { GetServerSideProps } from 'next'
import { host } from 'lib/config'
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
if (req.method !== 'GET') {
res.statusCode = 405
res.write({ 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

View File

@@ -1,15 +1,15 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { GetServerSideProps } from 'next'
import { SiteMap } from 'lib/types'
import { host } from 'lib/config'
import { getSiteMaps } from 'lib/get-site-maps'
import { SiteMap } from '../lib/types'
import { host } from '../lib/config'
import { getSiteMaps } from '../lib/get-site-maps'
export default async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> => {
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
if (req.method !== 'GET') {
return res.status(405).send({ error: 'method not allowed' })
res.statusCode = 405
res.write({ error: 'method not allowed' })
return {
props: {}
}
}
const siteMaps = await getSiteMaps()
@@ -22,6 +22,10 @@ export default async (
res.setHeader('Content-Type', 'text/xml')
res.write(createSitemap(siteMaps[0]))
res.end()
return {
props: {}
}
}
const createSitemap = (
@@ -47,3 +51,7 @@ const createSitemap = (
.join('')}
</urlset>
`
const SiteMapXml: React.FC = () => null
export default SiteMapXml