feat: add canonical URL handling and entity quality checks

- Implemented canonical URL strategies for city-specific domains and entities.
- Added helper functions for domain and city resolution.
- Created tests for canonical URL generation and domain resolution.
- Introduced entity quality checks for doctors and clinics to ensure meaningful content.
- Developed unique introductory texts for listing pages to avoid duplicate content.
- Established robots.txt policies for listing pages to manage indexing based on user filters.
- Enhanced specialty content with dynamic introductions and FAQs to improve SEO.
This commit is contained in:
hamed
2026-07-19 07:52:50 +03:30
parent 36816eded2
commit fd48b48613
48 changed files with 3951 additions and 733 deletions
+70 -11
View File
@@ -3,6 +3,9 @@ import { getBaseUrl } from '../utils/sitemap';
import citiesData from '@/data/city.json';
import statesData from '@/data/state.json';
import { isRootCity } from '@/lib/rootCity';
import { extractEntityCityId, findDomainByCityId } from '@/lib/domainHelpers';
import { isThinClinic, isThinDoctor } from '@/lib/entityQuality';
import specialtiesData from '@/data/specialties.json';
// sitemap باید در زمان اجرا (per-request) روی سرور production تولید شود، نه در build.
// در build کانتینر به API دسترسی شبکه ندارد → fetch failed. force-dynamic این را قطعی می‌کند.
@@ -11,8 +14,11 @@ export const revalidate = 3600;
const MAIN_DOMAIN = 'nobat724.com';
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const PAGE_LIMIT = 500;
const MAX_PAGES = 40;
// backend صفحه را به ۵۰ رکورد سقف می‌زند و limit بزرگ‌تر را نادیده می‌گیرد.
// شرط توقف باید meta.totalPages باشد، نه «کمتر از limit برگشت» — وگرنه حلقه در
// همان صفحهٔ اول می‌شکند و sitemap روی ۵۰ رکورد بریده می‌شود.
const PAGE_LIMIT = 50;
const MAX_PAGES = 200;
function toSafeDate(value) {
if (!value) return null;
@@ -78,8 +84,13 @@ async function fetchAllPages(path, extraParams = {}) {
const items = Array.isArray(raw) ? raw : Array.isArray(raw?.data) ? raw.data : [];
if (items.length === 0) break;
results.push(...items);
const total = json?.meta?.totalRecords;
if (items.length < PAGE_LIMIT || (total && results.length >= Number(total))) break;
const meta = json?.meta ?? raw?.meta;
const totalPages = Number(meta?.totalPages) || 0;
const totalRecords = Number(meta?.totalRecords) || 0;
if (totalPages && page >= totalPages) break;
if (totalRecords && results.length >= totalRecords) break;
// بدون meta تنها نشانهٔ پایان، صفحهٔ خالی است (در تکرار بعدی).
}
} catch (error) {
console.error(`[sitemap] failed to fetch ${path} (page skipped):`, error?.message || error);
@@ -95,10 +106,33 @@ function cityFilterParams(scope) {
return params;
}
// شهرهایی که دامنهٔ اختصاصی دارند — موجودیت‌هایشان canonical روی همان دامنه دارند و
// نباید در sitemap دامنهٔ اصلی تکرار شوند.
const CITY_IDS_WITH_DOMAIN = citiesData
.filter((c) => !isRootCity(c) && c.domain)
.map((c) => c.id);
/**
* پاسخ لیست پزشکان شهر ندارد؛ پس تفکیک با فیلتر سمت API انجام می‌شود:
* روی دامنهٔ شهری با city_id، و روی دامنهٔ اصلی «همه منهای پزشکانِ شهرهای دامنه‌دار».
*/
async function getDoctorsForScope(scope) {
if (!scope.isRoot) return fetchAllPages('/api/v1/doctors', cityFilterParams(scope));
const [all, ...perCity] = await Promise.all([
fetchAllPages('/api/v1/doctors'),
...CITY_IDS_WITH_DOMAIN.map((cityId) =>
fetchAllPages('/api/v1/doctors', { city_id: String(cityId) })
),
]);
const ownedByCityDomain = new Set(perCity.flat().map((d) => d?.uuid).filter(Boolean));
return all.filter((d) => !ownedByCityDomain.has(d?.uuid));
}
async function getDoctorUrls(baseUrl, scope) {
const doctors = await fetchAllPages('/api/v1/doctors', cityFilterParams(scope));
const doctors = await getDoctorsForScope(scope);
return doctors
.filter((d) => d?.uuid && d?.owner_status === 'claimed')
.filter((d) => !isThinDoctor(d))
.map((d) =>
withLastModified(
{
@@ -114,7 +148,9 @@ async function getDoctorUrls(baseUrl, scope) {
async function getClinicUrls(baseUrl, scope) {
const clinics = await fetchAllPages('/api/v1/clinics', cityFilterParams(scope));
return clinics
.filter((c) => c?.uuid)
.filter((c) => !isThinClinic(c))
// پاسخ لیست کلینیک‌ها شهر دارد — کلینیکِ شهرِ دامنه‌دار فقط در sitemap همان دامنه.
.filter((c) => !scope.isRoot || !findDomainByCityId(extractEntityCityId(c)))
.map((c) =>
withLastModified(
{
@@ -127,10 +163,27 @@ async function getClinicUrls(baseUrl, scope) {
);
}
async function getBlogUrls(baseUrl) {
// صفحات فرود تخصص per-domain — روی هر دامنه «تخصص + آن شهر» است، پس همه‌جا self.
function getSpecialtyUrls(baseUrl) {
return specialtiesData
.filter((s) => s?.slug && s.status === 1)
.map((s) => ({
url: `${baseUrl}/specialties/${s.slug}`,
changeFrequency: 'weekly',
priority: 0.8,
}));
}
// هر پست فقط در sitemap دامنهٔ canonical خودش: پست شهریافته → دامنهٔ آن شهر،
// پست سراسری (بدون شهر) → دامنهٔ اصلی.
async function getBlogUrls(baseUrl, scope, currentDomain) {
const blogs = await fetchAllPages('/api/v1/blogs');
return blogs
.filter((b) => b?.slug || b?.uuid)
.filter((b) => {
const cityDomain = findDomainByCityId(extractEntityCityId(b));
return cityDomain ? cityDomain === currentDomain : scope.isRoot;
})
.map((b) =>
withLastModified(
{
@@ -148,15 +201,21 @@ export default async function sitemap() {
const domain = await getCurrentDomain();
const baseUrl = getBaseUrl(domain);
const scope = getCityScope(domain);
const normalizedDomain = domain.toLowerCase().replace(/^www\./, '').split(':')[0];
const [doctorUrls, clinicUrls, blogUrls] = await Promise.all([
getDoctorUrls(baseUrl, scope),
getClinicUrls(baseUrl, scope),
// محتوای بلاگ روی همه‌ی دامنه‌ها یکسان است — فقط دامنه‌ی اصلی آن را در sitemap اعلام می‌کند
scope.isRoot ? getBlogUrls(baseUrl) : Promise.resolve([]),
getBlogUrls(baseUrl, scope, normalizedDomain),
]);
const allUrls = [...getStaticPages(baseUrl), ...doctorUrls, ...clinicUrls, ...blogUrls];
const allUrls = [
...getStaticPages(baseUrl),
...getSpecialtyUrls(baseUrl),
...doctorUrls,
...clinicUrls,
...blogUrls,
];
const seen = new Set();
return allUrls.filter((item) => {