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
+91 -1
View File
@@ -1,6 +1,12 @@
import { headers } from 'next/headers';
import { getBaseUrl } from '../utils/sitemap';
function toSafeDate(value) {
if (!value) return new Date();
const d = new Date(value);
return isNaN(d.getTime()) ? new Date() : d;
}
const MAIN_DOMAIN = 'nobat724.com';
function getCurrentDomain() {
@@ -27,11 +33,95 @@ function getStaticPages(baseUrl) {
];
}
async function getDoctorUrls(baseUrl) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
if (!API_URL) return [];
try {
const res = await fetch(`${API_URL}/api/v1/doctors?page=1&limit=2000`, {
next: { revalidate: 3600 },
});
if (!res.ok) return [];
const data = await res.json();
const doctors = data?.data || data?.doctors || data || [];
return doctors
.filter((d) => d?.uuid)
.map((d) => ({
url: `${baseUrl}/doctor/${d.uuid}`,
lastModified: NOW,
changeFrequency: 'weekly',
priority: 0.8,
}));
} catch {
return [];
}
}
async function getClinicUrls(baseUrl) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
if (!API_URL) return [];
try {
const res = await fetch(`${API_URL}/api/v1/clinics?page=1&limit=2000`, {
next: { revalidate: 3600 },
});
if (!res.ok) return [];
const data = await res.json();
const clinics = data?.data || data?.clinics || data || [];
return clinics
.filter((c) => c?.uuid)
.map((c) => ({
url: `${baseUrl}/clinic/${c.uuid}`,
lastModified: NOW,
changeFrequency: 'weekly',
priority: 0.7,
}));
} catch {
return [];
}
}
async function getBlogUrls(baseUrl) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
if (!API_URL) return [];
try {
const res = await fetch(`${API_URL}/api/v1/blogs?page=1&limit=2000`, {
next: { revalidate: 3600 },
});
if (!res.ok) return [];
const data = await res.json();
const blogs = data?.blogs || data?.data || data || [];
return blogs
.filter((b) => b?.slug || b?.uuid)
.map((b) => ({
url: `${baseUrl}/blog/${b.slug || b.uuid}`,
lastModified: toSafeDate(b.created),
changeFrequency: 'monthly',
priority: 0.6,
}));
} catch {
return [];
}
}
export default async function sitemap() {
try {
const domain = getCurrentDomain();
const baseUrl = getBaseUrl(domain);
return getStaticPages(baseUrl);
const [staticPages, doctorUrls, clinicUrls, blogUrls] = await Promise.all([
Promise.resolve(getStaticPages(baseUrl)),
getDoctorUrls(baseUrl),
getClinicUrls(baseUrl),
getBlogUrls(baseUrl),
]);
const allUrls = [...staticPages, ...doctorUrls, ...clinicUrls, ...blogUrls];
return allUrls.filter(
(item, index, arr) => arr.findIndex((i) => i.url === item.url) === index
);
} catch (error) {
console.error('Error generating sitemap:', error);
return [];