From fe7b68a824234ec21803233f06129ce2a44e4b86 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Fri, 8 Nov 2024 11:10:09 +0100 Subject: [PATCH] feat: add robots.txt and sitemap --- app/robots.js | 9 +++++++++ app/sitemap.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 app/robots.js create mode 100644 app/sitemap.js diff --git a/app/robots.js b/app/robots.js new file mode 100644 index 0000000..98600c7 --- /dev/null +++ b/app/robots.js @@ -0,0 +1,9 @@ +export default function robots() { + return { + rules: { + userAgent: "*", + allow: "/" + }, + sitemap: `${process.env.NEXT_PUBLIC_WEBSITE_URL}/sitemap.xml` + }; +} diff --git a/app/sitemap.js b/app/sitemap.js new file mode 100644 index 0000000..ff44042 --- /dev/null +++ b/app/sitemap.js @@ -0,0 +1,37 @@ +import { client } from "@/sanity/lib/client"; +import docLinks from "@/constants/docLinks"; + +async function getAllBlogPostSlugs() { + const query = `*[_type == 'blog'] { "slug": slug.current }`; + const slugs = await client.fetch(query, {}, { cache: "no-store" }); + + return slugs; +} + +export default async function sitemap() { + const blogPostSlugs = await getAllBlogPostSlugs(); + + const baseRoutes = [ + "/", + "/blog", + "/contact", + "/contribute", + "/privacy", + "/tos" + ].map((route) => ({ + url: `${process.env.NEXT_PUBLIC_WEBSITE_URL}${route}`, + lastModified: new Date().toISOString().split("T")[0] + })); + + const docsRoutes = docLinks.map((docLink) => ({ + url: `${process.env.NEXT_PUBLIC_WEBSITE_URL}${docLink.href}`, + lastModified: new Date().toISOString().split("T")[0] + })); + + const blogRoutes = blogPostSlugs.map((slug) => ({ + url: `${process.env.NEXT_PUBLIC_WEBSITE_URL}/blog/${slug.slug}`, + lastModified: new Date().toISOString().split("T")[0] + })); + + return [...baseRoutes, ...docsRoutes, ...blogRoutes]; +}