feat(sitemap): refactor sitemap generation to include doctors, clinics, and blogs with improved URL handling

This commit is contained in:
hamed
2026-06-21 12:52:41 +03:30
parent a8c3a57114
commit cb9d9beaab
6 changed files with 98 additions and 141 deletions
-49
View File
@@ -1,49 +0,0 @@
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const MAIN_DOMAIN = 'https://nobat724.com';
const PER_SITEMAP = 1000;
function toSafeDate(value) {
if (!value) return new Date();
const d = new Date(value);
return isNaN(d.getTime()) ? new Date() : d;
}
export async function generateSitemaps() {
if (!API_URL) return [{ id: 0 }];
try {
const res = await fetch(`${API_URL}/api/v1/blogs?page=1&limit=1`, {
next: { revalidate: 86400 },
});
if (!res.ok) return [{ id: 0 }];
const json = await res.json();
const total = json?.meta?.totalRecords || 0;
const pageCount = Math.max(1, Math.ceil(total / PER_SITEMAP));
return Array.from({ length: pageCount }, (_, id) => ({ id }));
} catch {
return [{ id: 0 }];
}
}
export default async function sitemap({ id }) {
if (!API_URL) return [];
try {
const res = await fetch(`${API_URL}/api/v1/blogs?page=${id + 1}&limit=${PER_SITEMAP}`, {
next: { revalidate: 3600 },
});
if (!res.ok) return [];
const json = await res.json();
const blogs = json?.data || [];
return blogs
.filter((b) => b?.slug || b?.uuid)
.map((b) => ({
url: `${MAIN_DOMAIN}/blog/${b.slug || b.uuid}`,
lastModified: toSafeDate(b.created),
changeFrequency: 'monthly',
priority: 0.6,
}));
} catch {
return [];
}
}