fix: sitemap and robots werent building properly

This commit is contained in:
Travis Fischer
2021-02-06 00:30:43 -05:00
parent 70d674d1d8
commit afda065a24
5 changed files with 76 additions and 50 deletions

23
api/robots.txt.ts Normal file
View File

@@ -0,0 +1,23 @@
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' })
}
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: *
Allow: /
Sitemap: ${host}/api/sitemap.xml
`)
res.end()
}

45
api/sitemap.xml.ts Normal file
View File

@@ -0,0 +1,45 @@
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()
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>
<loc>${host}/</loc>
</url>
${Object.keys(siteMap.canonicalPageMap)
.map((canonicalPagePath) =>
`
<url>
<loc>${host}/${canonicalPagePath}</loc>
</url>
`.trim()
)
.join('')}
</urlset>
`

View File

@@ -8,6 +8,14 @@ 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 }

View File

@@ -1,14 +0,0 @@
import React from 'react'
import { host } from 'lib/config'
export default class Robots extends React.Component {
static async getInitialProps({ res }) {
res.setHeader('Content-Type', 'text/plain')
res.write(`User-agent: *
Allow: /
Sitemap: ${host}/sitemap.xml
`)
res.end()
}
}

View File

@@ -1,36 +0,0 @@
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()
}
}