diff --git a/pages/[pageId].tsx b/pages/[pageId].tsx index 15a093f..64f0200 100644 --- a/pages/[pageId].tsx +++ b/pages/[pageId].tsx @@ -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 } diff --git a/pages/robots.txt.tsx b/pages/robots.txt.tsx new file mode 100644 index 0000000..e79c843 --- /dev/null +++ b/pages/robots.txt.tsx @@ -0,0 +1,31 @@ +import { GetServerSideProps } from 'next' +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.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 diff --git a/pages/sitemap.xml.tsx b/pages/sitemap.xml.tsx new file mode 100644 index 0000000..6c30ae6 --- /dev/null +++ b/pages/sitemap.xml.tsx @@ -0,0 +1,59 @@ +import { GetServerSideProps } from 'next' +import { SiteMap } from 'lib/types' +import { host } from 'lib/config' +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.end() + return { + props: {} + } + } + + 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() + + return { + props: {} + } +} + +const createSitemap = ( + siteMap: SiteMap +) => ` + + + ${host} + + + + ${host}/ + + + ${Object.keys(siteMap.canonicalPageMap) + .map((canonicalPagePath) => + ` + + ${host}/${canonicalPagePath} + + `.trim() + ) + .join('')} + + ` + +const SiteMapXml: React.FC = () => null + +export default SiteMapXml