feat(sitemap): refactor sitemap generation to include doctors, clinics, and blogs with improved URL handling
This commit is contained in:
@@ -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 [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL;
|
|
||||||
const MAIN_DOMAIN = 'https://nobat724.com';
|
|
||||||
const PER_SITEMAP = 1000;
|
|
||||||
|
|
||||||
export async function generateSitemaps() {
|
|
||||||
if (!API_URL) return [{ id: 0 }];
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/api/v1/clinics?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/clinics?page=${id + 1}&limit=${PER_SITEMAP}`, {
|
|
||||||
next: { revalidate: 3600 },
|
|
||||||
});
|
|
||||||
if (!res.ok) return [];
|
|
||||||
const json = await res.json();
|
|
||||||
const clinics = json?.data || [];
|
|
||||||
return clinics
|
|
||||||
.filter((c) => c?.uuid)
|
|
||||||
.map((c) => ({
|
|
||||||
url: `${MAIN_DOMAIN}/clinic/${c.uuid}`,
|
|
||||||
lastModified: new Date(),
|
|
||||||
changeFrequency: 'weekly',
|
|
||||||
priority: 0.7,
|
|
||||||
}));
|
|
||||||
} catch {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -47,7 +47,7 @@ export async function generateMetadata({ params }) {
|
|||||||
const description =
|
const description =
|
||||||
doctor.detail ||
|
doctor.detail ||
|
||||||
`رزرو نوبت آنلاین دکتر ${doctor.name}${specialtyNames ? " متخصص " + specialtyNames : ""}${doctor.address ? " | " + doctor.address : ""}`.trim();
|
`رزرو نوبت آنلاین دکتر ${doctor.name}${specialtyNames ? " متخصص " + specialtyNames : ""}${doctor.address ? " | " + doctor.address : ""}`.trim();
|
||||||
const image = imageUrl(doctor.img) || FALLBACK_IMG;
|
const image = imageUrl(doctor.img?.[0]?.url) || FALLBACK_IMG;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title,
|
title,
|
||||||
@@ -103,10 +103,10 @@ async function Doctor({ params }) {
|
|||||||
name: `دکتر ${doctor.name}`,
|
name: `دکتر ${doctor.name}`,
|
||||||
url: `https://www.nobat724.com/doctor/${doctor.uuid}`,
|
url: `https://www.nobat724.com/doctor/${doctor.uuid}`,
|
||||||
...(specialtyNames && { medicalSpecialty: specialtyNames }),
|
...(specialtyNames && { medicalSpecialty: specialtyNames }),
|
||||||
...(doctor.img && {
|
...(doctor.img?.[0]?.url && {
|
||||||
image: {
|
image: {
|
||||||
"@type": "ImageObject",
|
"@type": "ImageObject",
|
||||||
url: imageUrl(doctor.img),
|
url: imageUrl(doctor.img[0].url),
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
...(Number(doctor.point) > 0 && {
|
...(Number(doctor.point) > 0 && {
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL;
|
|
||||||
const MAIN_DOMAIN = 'https://nobat724.com';
|
|
||||||
const PER_SITEMAP = 1000;
|
|
||||||
|
|
||||||
export async function generateSitemaps() {
|
|
||||||
if (!API_URL) return [{ id: 0 }];
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/api/v1/doctors?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/doctors?page=${id + 1}&limit=${PER_SITEMAP}`, {
|
|
||||||
next: { revalidate: 3600 },
|
|
||||||
});
|
|
||||||
if (!res.ok) return [];
|
|
||||||
const json = await res.json();
|
|
||||||
const doctors = json?.data || [];
|
|
||||||
return doctors
|
|
||||||
.filter((d) => d?.uuid)
|
|
||||||
.map((d) => ({
|
|
||||||
url: `${MAIN_DOMAIN}/doctor/${d.uuid}`,
|
|
||||||
lastModified: new Date(),
|
|
||||||
changeFrequency: 'weekly',
|
|
||||||
priority: 0.8,
|
|
||||||
}));
|
|
||||||
} catch {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+91
-1
@@ -1,6 +1,12 @@
|
|||||||
import { headers } from 'next/headers';
|
import { headers } from 'next/headers';
|
||||||
import { getBaseUrl } from '../utils/sitemap';
|
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';
|
const MAIN_DOMAIN = 'nobat724.com';
|
||||||
|
|
||||||
function getCurrentDomain() {
|
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() {
|
export default async function sitemap() {
|
||||||
try {
|
try {
|
||||||
const domain = getCurrentDomain();
|
const domain = getCurrentDomain();
|
||||||
const baseUrl = getBaseUrl(domain);
|
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) {
|
} catch (error) {
|
||||||
console.error('Error generating sitemap:', error);
|
console.error('Error generating sitemap:', error);
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ function cookieDomain(host) {
|
|||||||
return "." + hostname.split(".").slice(-2).join(".");
|
return "." + hostname.split(".").slice(-2).join(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isSecure = process.env.NODE_ENV === "production";
|
||||||
|
|
||||||
export function setRefreshCookie(response, refreshToken, host, maxAge = 60 * 60 * 24 * 30) {
|
export function setRefreshCookie(response, refreshToken, host, maxAge = 60 * 60 * 24 * 30) {
|
||||||
response.cookies.set(COOKIE_NAME, refreshToken, {
|
response.cookies.set(COOKIE_NAME, refreshToken, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: isSecure,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge,
|
maxAge,
|
||||||
@@ -23,7 +25,7 @@ export function setRefreshCookie(response, refreshToken, host, maxAge = 60 * 60
|
|||||||
export function clearRefreshCookie(response, host) {
|
export function clearRefreshCookie(response, host) {
|
||||||
response.cookies.set(COOKIE_NAME, "", {
|
response.cookies.set(COOKIE_NAME, "", {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: isSecure,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: 0,
|
maxAge: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user