From 2101117582237a5ae812e44d611ad47761026ee9 Mon Sep 17 00:00:00 2001 From: Mohammed Rayan A Date: Mon, 20 Jul 2026 23:23:16 +0530 Subject: [PATCH] feat: implement robots.txt and dynamic sitemap.xml --- app/robots.ts | 13 +++++++++++++ app/sitemap.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/robots.ts create mode 100644 app/sitemap.ts diff --git a/app/robots.ts b/app/robots.ts new file mode 100644 index 0000000..b41f1eb --- /dev/null +++ b/app/robots.ts @@ -0,0 +1,13 @@ +import { MetadataRoute } from 'next' + +export default function robots(): MetadataRoute.Robots { + const baseUrl = process.env.NEXT_PUBLIC_APP_URL || 'https://dumpit-three.vercel.app' + return { + rules: { + userAgent: '*', + allow: '/', + disallow: ['/dashboard/', '/api/', '/login/'], + }, + sitemap: `${baseUrl}/sitemap.xml`, + } +} diff --git a/app/sitemap.ts b/app/sitemap.ts new file mode 100644 index 0000000..3146bf4 --- /dev/null +++ b/app/sitemap.ts @@ -0,0 +1,47 @@ +import { MetadataRoute } from 'next' +import { getServerFirestore } from './api/_utils/firebaseAdmin' + +export default async function sitemap(): Promise { + const baseUrl = process.env.NEXT_PUBLIC_APP_URL || 'https://dumpit-three.vercel.app' + + let userUrls: MetadataRoute.Sitemap = [] + try { + const db = getServerFirestore() + const usersSnapshot = await db.collection('users').get() + + userUrls = usersSnapshot.docs + .map((doc) => { + const data = doc.data() + const username = data.username + if (!username) return null + + let lastModDate = new Date() + if (data.updated_at) { + if (typeof data.updated_at.toDate === 'function') { + lastModDate = data.updated_at.toDate() + } else { + lastModDate = new Date(data.updated_at) + } + } + + return { + url: `${baseUrl}/u/${username}`, + lastModified: lastModDate, + changeFrequency: 'weekly' as const, + priority: 0.8, + } + }) + .filter((url): url is Exclude => url !== null) + } catch (error) { + console.error('Error generating sitemap dynamic URLs:', error) + } + + const staticUrls = ['', '/privacy', '/terms'].map((route) => ({ + url: `${baseUrl}${route}`, + lastModified: new Date(), + changeFrequency: 'monthly' as const, + priority: route === '' ? 1.0 : 0.3, + })) + + return [...staticUrls, ...userUrls] +}