From 523846180e16ff941f4c728265fe306c1c4629bf Mon Sep 17 00:00:00 2001 From: yyosifov Date: Thu, 7 Dec 2023 13:35:00 +0200 Subject: [PATCH] make the robots.txt file dynamic (#1680) --- next.config.js | 4 ++++ public/robots.txt | 9 --------- src/pages/api/robots.ts | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) delete mode 100644 public/robots.txt create mode 100644 src/pages/api/robots.ts diff --git a/next.config.js b/next.config.js index a4e116a7b..963dfefc6 100644 --- a/next.config.js +++ b/next.config.js @@ -51,6 +51,10 @@ const moduleExports = { source: '/api/v1/:slug*', destination: `${process.env.API_URL ?? 'http://localhost:5010/api/v1'}/:slug*`, // Proxy to API }, + { + source: '/robots.txt', + destination: '/api/robots' + } ] }, async redirects() { diff --git a/public/robots.txt b/public/robots.txt deleted file mode 100644 index ed48c036e..000000000 --- a/public/robots.txt +++ /dev/null @@ -1,9 +0,0 @@ -# * -User-agent: * -Allow: / - -# Host -Host: https://podkrepi.bg - -# Sitemaps -Sitemap: https://podkrepi.bg/sitemap.xml diff --git a/src/pages/api/robots.ts b/src/pages/api/robots.ts new file mode 100644 index 000000000..2596acc1e --- /dev/null +++ b/src/pages/api/robots.ts @@ -0,0 +1,24 @@ +import type { NextApiRequest, NextApiResponse } from 'next' + +const productionRobotsContent = `# * +User-agent: * +Allow: / + +# Host +Host: https://podkrepi.bg + +# Sitemaps +Sitemap: https://podkrepi.bg/sitemap.xml +` + +const devRobotsContent = `User-agent: * +Disallow: / +` + +export default async (req: NextApiRequest, res: NextApiResponse) => { + if (process.env.APP_ENV === 'production') { + res.status(200).send(productionRobotsContent) + } else { + res.status(200).send(devRobotsContent) + } +}