From d1e495e000b7ca8f744913beacbec572950fc4d6 Mon Sep 17 00:00:00 2001 From: JaeSeoKim Date: Sun, 17 Oct 2021 00:44:46 +0900 Subject: [PATCH] feat: Change sitemap.xml, robots.xml end-point --- api/robots.txt.ts | 23 ---------------- pages/[pageId].tsx | 8 ------ pages/robots.txt.tsx | 30 +++++++++++++++++++++ api/sitemap.xml.ts => pages/sitemap.xml.tsx | 28 ++++++++++++------- 4 files changed, 48 insertions(+), 41 deletions(-) delete mode 100644 api/robots.txt.ts create mode 100644 pages/robots.txt.tsx rename api/sitemap.xml.ts => pages/sitemap.xml.tsx (65%) diff --git a/api/robots.txt.ts b/api/robots.txt.ts deleted file mode 100644 index f4e9355..0000000 --- a/api/robots.txt.ts +++ /dev/null @@ -1,23 +0,0 @@ -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' }) - } - - // 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() -} 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..cf5273e --- /dev/null +++ b/pages/robots.txt.tsx @@ -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 diff --git a/api/sitemap.xml.ts b/pages/sitemap.xml.tsx similarity index 65% rename from api/sitemap.xml.ts rename to pages/sitemap.xml.tsx index f591ed8..5f18def 100644 --- a/api/sitemap.xml.ts +++ b/pages/sitemap.xml.tsx @@ -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 => { +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('')} ` + +const SiteMapXml: React.FC = () => null + +export default SiteMapXml