130 lines
4.0 KiB
JavaScript
130 lines
4.0 KiB
JavaScript
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() {
|
|
try {
|
|
const headersList = headers();
|
|
const host = headersList.get('host');
|
|
return host || MAIN_DOMAIN;
|
|
} catch {
|
|
return MAIN_DOMAIN;
|
|
}
|
|
}
|
|
|
|
const NOW = new Date();
|
|
|
|
function getStaticPages(baseUrl) {
|
|
return [
|
|
{ url: baseUrl, lastModified: NOW, changeFrequency: 'daily', priority: 1 },
|
|
{ url: `${baseUrl}/about-us`, lastModified: NOW, changeFrequency: 'monthly', priority: 0.8 },
|
|
{ url: `${baseUrl}/contact-us`, lastModified: NOW, changeFrequency: 'monthly', priority: 0.8 },
|
|
{ url: `${baseUrl}/blogs`, lastModified: NOW, changeFrequency: 'weekly', priority: 0.9 },
|
|
{ url: `${baseUrl}/doctors`, lastModified: NOW, changeFrequency: 'daily', priority: 0.9 },
|
|
{ url: `${baseUrl}/clinics`, lastModified: NOW, changeFrequency: 'daily', priority: 0.9 },
|
|
{ url: `${baseUrl}/specialties`, lastModified: NOW, 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=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);
|
|
|
|
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 [];
|
|
}
|
|
}
|