From d1e495e000b7ca8f744913beacbec572950fc4d6 Mon Sep 17 00:00:00 2001 From: JaeSeoKim Date: Sun, 17 Oct 2021 00:44:46 +0900 Subject: [PATCH 1/4] 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 From 37411d7330781100b8c2b1924682a5b171bb0429 Mon Sep 17 00:00:00 2001 From: JaeSeoKim Date: Sun, 17 Oct 2021 01:11:30 +0900 Subject: [PATCH 2/4] fix: sitemap.xml.tsx --- pages/sitemap.xml.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/sitemap.xml.tsx b/pages/sitemap.xml.tsx index 5f18def..7780913 100644 --- a/pages/sitemap.xml.tsx +++ b/pages/sitemap.xml.tsx @@ -7,6 +7,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { if (req.method !== 'GET') { res.statusCode = 405 res.write({ error: 'method not allowed' }) + res.end() return { props: {} } From 882a815106abddb261b166057ceca9e6253c6e4f Mon Sep 17 00:00:00 2001 From: JaeSeoKim Date: Mon, 18 Oct 2021 13:26:55 +0900 Subject: [PATCH 3/4] Fix: robots.txt wrong method response --- pages/robots.txt.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pages/robots.txt.tsx b/pages/robots.txt.tsx index cf5273e..e79c843 100644 --- a/pages/robots.txt.tsx +++ b/pages/robots.txt.tsx @@ -4,7 +4,8 @@ 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.setHeader("Content-Type", "application/json") + res.write(JSON.stringify({ error: "method not allowed" })) res.end() return { props: {} From d5036a4359eba2d810f55341ea931595cbb092c2 Mon Sep 17 00:00:00 2001 From: JaeSeoKim Date: Mon, 18 Oct 2021 13:27:37 +0900 Subject: [PATCH 4/4] Fix: sitemap.xml wrong method response --- pages/sitemap.xml.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pages/sitemap.xml.tsx b/pages/sitemap.xml.tsx index 7780913..6c30ae6 100644 --- a/pages/sitemap.xml.tsx +++ b/pages/sitemap.xml.tsx @@ -6,7 +6,8 @@ import { getSiteMaps } from 'lib/get-site-maps' export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { if (req.method !== 'GET') { res.statusCode = 405 - res.write({ error: 'method not allowed' }) + res.setHeader("Content-Type", "application/json") + res.write(JSON.stringify({ error: "method not allowed" })) res.end() return { props: {}