diff --git a/api/robots.txt.ts b/api/robots.txt.ts new file mode 100644 index 0000000..b8115f8 --- /dev/null +++ b/api/robots.txt.ts @@ -0,0 +1,23 @@ +import { NextApiRequest, NextApiResponse } from 'next' + +import { host } from '../lib/config' + +export default async ( + req: NextApiRequest, + res: NextApiResponse +): Promise => { + 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() +} diff --git a/api/sitemap.xml.ts b/api/sitemap.xml.ts new file mode 100644 index 0000000..5fca792 --- /dev/null +++ b/api/sitemap.xml.ts @@ -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 => { + 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 +) => ` + + + ${host} + ${host}/ + + + ${Object.keys(siteMap.canonicalPageMap) + .map((canonicalPagePath) => + ` + + ${host}/${canonicalPagePath} + + `.trim() + ) + .join('')} + + ` diff --git a/pages/[pageId].tsx b/pages/[pageId].tsx index e81231a..afbbaae 100644 --- a/pages/[pageId].tsx +++ b/pages/[pageId].tsx @@ -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 } diff --git a/pages/robots.txt.tsx b/pages/robots.txt.tsx deleted file mode 100644 index cf85d6a..0000000 --- a/pages/robots.txt.tsx +++ /dev/null @@ -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() - } -} diff --git a/pages/sitemap.xml.tsx b/pages/sitemap.xml.tsx deleted file mode 100644 index a77e159..0000000 --- a/pages/sitemap.xml.tsx +++ /dev/null @@ -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 -) => ` - - - ${host} - ${host}/ - - - ${Object.keys(siteMap.canonicalPageMap) - .map((canonicalPagePath) => - ` - - ${host}/${canonicalPagePath} - - `.trim() - ) - .join('')} - - ` - -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() - } -}