mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-14 07:26:43 +00:00
feat: optimize robots.txt and sitemap.xml
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()
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
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> => {
|
||||
if (req.method !== 'GET') {
|
||||
return res.status(405).send({ error: 'method not allowed' })
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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>
|
||||
</xml>
|
||||
`
|
||||
@@ -4,9 +4,10 @@ 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.setHeader('Content-Type', 'application/json')
|
||||
res.write(JSON.stringify({ error: 'method not allowed' }))
|
||||
res.end()
|
||||
|
||||
return {
|
||||
props: {}
|
||||
}
|
||||
@@ -14,18 +15,31 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
'public, s-maxage=60, max-age=60, stale-while-revalidate=60'
|
||||
'public, s-maxage=3600, max-age=3600, stale-while-revalidate=3600'
|
||||
)
|
||||
res.setHeader('Content-Type', 'text/plain')
|
||||
res.write(`User-agent: *
|
||||
Sitemap: ${host}/sitemap.xml
|
||||
`)
|
||||
|
||||
// only allow the site to be crawlable on the production deployment
|
||||
if (process.env.VERCEL_ENV === 'production') {
|
||||
res.write(`User-agent: *
|
||||
Allow: /
|
||||
Disallow: /api/*
|
||||
|
||||
Sitemap: ${host}/sitemap.xml
|
||||
`)
|
||||
} else {
|
||||
res.write(`User-agent: *
|
||||
Disallow: /
|
||||
|
||||
Sitemap: ${host}/sitemap.xml
|
||||
`)
|
||||
}
|
||||
|
||||
res.end()
|
||||
|
||||
return {
|
||||
props: {}
|
||||
}
|
||||
}
|
||||
|
||||
const RobotsTxt: React.FC = () => null
|
||||
|
||||
export default RobotsTxt
|
||||
export default () => null
|
||||
|
||||
@@ -6,8 +6,8 @@ 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.setHeader('Content-Type', 'application/json')
|
||||
res.write(JSON.stringify({ error: 'method not allowed' }))
|
||||
res.end()
|
||||
return {
|
||||
props: {}
|
||||
@@ -16,7 +16,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||
|
||||
const siteMaps = await getSiteMaps()
|
||||
|
||||
// cache sitemap for up to one hour
|
||||
// cache for up to one hour
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
'public, s-maxage=3600, max-age=3600, stale-while-revalidate=3600'
|
||||
@@ -30,30 +30,27 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
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>
|
||||
<url>
|
||||
<loc>${host}/</loc>
|
||||
</url>
|
||||
|
||||
${Object.keys(siteMap.canonicalPageMap)
|
||||
.map((canonicalPagePath) =>
|
||||
`
|
||||
<url>
|
||||
<loc>${host}/${canonicalPagePath}</loc>
|
||||
</url>
|
||||
`.trim()
|
||||
)
|
||||
.join('')}
|
||||
</urlset>
|
||||
`
|
||||
${Object.keys(siteMap.canonicalPageMap)
|
||||
.map((canonicalPagePath) =>
|
||||
`
|
||||
<url>
|
||||
<loc>${host}/${canonicalPagePath}</loc>
|
||||
</url>
|
||||
`.trim()
|
||||
)
|
||||
.join('')}
|
||||
</urlset>
|
||||
`
|
||||
|
||||
const SiteMapXml: React.FC = () => null
|
||||
|
||||
export default SiteMapXml
|
||||
export default () => null
|
||||
|
||||
Reference in New Issue
Block a user