134 lines
4.3 KiB
JavaScript
134 lines
4.3 KiB
JavaScript
import { headers } from 'next/headers';
|
|
import cityData from '../data/city.json';
|
|
import specialtyData from '../data/specialties.json';
|
|
import {
|
|
DOMAIN_CONFIG,
|
|
isMainDomain,
|
|
createSitemapUrl,
|
|
createSitemapEntry,
|
|
limitArray,
|
|
getBaseUrl
|
|
} from '../utils/sitemap';
|
|
|
|
function getCurrentDomain() {
|
|
try {
|
|
const headersList = headers();
|
|
const host = headersList.get('host');
|
|
return host || DOMAIN_CONFIG.MAIN_DOMAIN;
|
|
} catch (error) {
|
|
console.warn('Headers not available during static generation, using main domain');
|
|
return DOMAIN_CONFIG.MAIN_DOMAIN;
|
|
}
|
|
}
|
|
|
|
function getCityByDomain(domain) {
|
|
return cityData.find(city => city.domain === domain);
|
|
}
|
|
|
|
function getStaticPages(baseUrl) {
|
|
return [
|
|
{ url: baseUrl, lastModified: new Date(), changeFrequency: 'daily', priority: 1 },
|
|
{ url: `${baseUrl}/about-us`, lastModified: new Date(), changeFrequency: 'monthly', priority: 0.8 },
|
|
{ url: `${baseUrl}/contact-us`, lastModified: new Date(), changeFrequency: 'monthly', priority: 0.8 },
|
|
{ url: `${baseUrl}/blogs`, lastModified: new Date(), changeFrequency: 'weekly', priority: 0.9 },
|
|
{ url: `${baseUrl}/doctors`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 },
|
|
{ url: `${baseUrl}/clinics`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 },
|
|
{ url: `${baseUrl}/specialties`, lastModified: new Date(), changeFrequency: 'weekly', priority: 0.9 },
|
|
];
|
|
}
|
|
|
|
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=500`, {
|
|
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: new Date(),
|
|
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=500`, {
|
|
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: new Date(),
|
|
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=200`, {
|
|
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: b.created ? new Date(b.created) : new Date(),
|
|
changeFrequency: 'monthly',
|
|
priority: 0.6,
|
|
}));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export default async function sitemap() {
|
|
try {
|
|
const domain = getCurrentDomain();
|
|
const baseUrl = getBaseUrl(domain);
|
|
|
|
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 [];
|
|
}
|
|
}
|