mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 23:16:47 +00:00
feat: Change sitemap.xml, robots.xml end-point
This commit is contained in:
@@ -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()
|
||||
}
|
||||
@@ -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
30
pages/robots.txt.tsx
Normal 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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user