Implement multi-domain SEO improvements:

- Add metadata to login and login-verify pages to prevent indexing.
- Update robots.txt to disallow additional sensitive paths.
- Enhance sitemap generation to filter by city and include accurate last modified dates.
- Refactor canonical URL generation to support multi-domain architecture, ensuring self-canonicalization for city domains.
- Remove deprecated CanonicalHandler component and streamline canonical URL handling.
- Introduce safe JSON-LD output to prevent XSS vulnerabilities.
- Add payment layout with appropriate metadata to prevent indexing.
- Conduct a comprehensive technical SEO audit and implement necessary fixes across the application.
This commit is contained in:
hamed
2026-07-05 15:47:47 +03:30
parent 522caf6b1b
commit b04bb45ca1
24 changed files with 595 additions and 329 deletions
+125 -89
View File
@@ -1,127 +1,163 @@
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;
}
import citiesData from '@/data/city.json';
import statesData from '@/data/state.json';
import { isRootCity } from '@/lib/rootCity';
const MAIN_DOMAIN = 'nobat724.com';
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const PAGE_LIMIT = 500;
const MAX_PAGES = 40;
function getCurrentDomain() {
function toSafeDate(value) {
if (!value) return null;
const d = new Date(typeof value === 'number' ? value * 1000 : value);
return isNaN(d.getTime()) ? null : d;
}
function withLastModified(entry, dateValue) {
const lastModified = toSafeDate(dateValue);
return lastModified ? { ...entry, lastModified } : entry;
}
async function getCurrentDomain() {
try {
const headersList = headers();
const host = headersList.get('host');
return host || MAIN_DOMAIN;
const headersList = await headers();
return headersList.get('host') || MAIN_DOMAIN;
} catch {
return MAIN_DOMAIN;
}
}
const NOW = new Date();
// همان منطق lib/getStateInfo.js — اینجا host را از آرگومان می‌گیریم نه دوباره از headers
function getCityScope(host) {
const subdomain = (host || '').toLowerCase().replace(/^www\./, '').split('.')[0];
const matchedCity = citiesData.find(
(city) => city.domain.split('.')[0] === subdomain
);
const matchedState =
matchedCity && statesData.find((state) => state.id === matchedCity.province_id);
const isRoot = !matchedCity || isRootCity(matchedCity);
return { matchedCity, matchedState, isRoot };
}
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 },
{ url: baseUrl, changeFrequency: 'daily', priority: 1 },
{ url: `${baseUrl}/about-us`, changeFrequency: 'monthly', priority: 0.8 },
{ url: `${baseUrl}/contact-us`, changeFrequency: 'monthly', priority: 0.8 },
{ url: `${baseUrl}/blogs`, changeFrequency: 'weekly', priority: 0.9 },
{ url: `${baseUrl}/doctors`, changeFrequency: 'daily', priority: 0.9 },
{ url: `${baseUrl}/clinics`, changeFrequency: 'daily', priority: 0.9 },
{ url: `${baseUrl}/specialties`, changeFrequency: 'weekly', priority: 0.9 },
];
}
async function getDoctorUrls(baseUrl) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
async function fetchAllPages(path, extraParams = {}) {
if (!API_URL) return [];
const results = [];
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 [];
for (let page = 1; page <= MAX_PAGES; page++) {
const search = new URLSearchParams({
...extraParams,
page: String(page),
limit: String(PAGE_LIMIT),
});
const res = await fetch(`${API_URL}${path}?${search.toString()}`, {
next: { revalidate: 3600 },
});
if (!res.ok) break;
const json = await res.json();
const raw = json?.data ?? json;
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;
}
} catch (error) {
console.error(`Error fetching sitemap data from ${path}:`, error);
}
return results;
}
async function getClinicUrls(baseUrl) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
if (!API_URL) return [];
function cityFilterParams(scope) {
if (scope.isRoot) return {};
const params = {};
if (scope.matchedState?.id) params.state_id = String(scope.matchedState.id);
if (scope.matchedCity?.id) params.city_id = String(scope.matchedCity.id);
return params;
}
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 getDoctorUrls(baseUrl, scope) {
const doctors = await fetchAllPages('/api/v1/doctors', cityFilterParams(scope));
return doctors
.filter((d) => d?.uuid)
.map((d) =>
withLastModified(
{
url: `${baseUrl}/doctor/${d.uuid}`,
changeFrequency: 'weekly',
priority: 0.8,
},
d.updated || d.created
)
);
}
async function getClinicUrls(baseUrl, scope) {
const clinics = await fetchAllPages('/api/v1/clinics', cityFilterParams(scope));
return clinics
.filter((c) => c?.uuid)
.map((c) =>
withLastModified(
{
url: `${baseUrl}/clinic/${c.uuid}`,
changeFrequency: 'weekly',
priority: 0.7,
},
c.updated || c.created
)
);
}
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 [];
}
const blogs = await fetchAllPages('/api/v1/blogs');
return blogs
.filter((b) => b?.slug || b?.uuid)
.map((b) =>
withLastModified(
{
url: `${baseUrl}/blog/${b.slug || b.uuid}`,
changeFrequency: 'monthly',
priority: 0.6,
},
b.updated || b.created
)
);
}
export default async function sitemap() {
try {
const domain = getCurrentDomain();
const domain = await getCurrentDomain();
const baseUrl = getBaseUrl(domain);
const scope = getCityScope(domain);
const [staticPages, doctorUrls, clinicUrls, blogUrls] = await Promise.all([
Promise.resolve(getStaticPages(baseUrl)),
getDoctorUrls(baseUrl),
getClinicUrls(baseUrl),
getBlogUrls(baseUrl),
const [doctorUrls, clinicUrls, blogUrls] = await Promise.all([
getDoctorUrls(baseUrl, scope),
getClinicUrls(baseUrl, scope),
// محتوای بلاگ روی همه‌ی دامنه‌ها یکسان است — فقط دامنه‌ی اصلی آن را در sitemap اعلام می‌کند
scope.isRoot ? getBlogUrls(baseUrl) : Promise.resolve([]),
]);
const allUrls = [...staticPages, ...doctorUrls, ...clinicUrls, ...blogUrls];
const allUrls = [...getStaticPages(baseUrl), ...doctorUrls, ...clinicUrls, ...blogUrls];
return allUrls.filter(
(item, index, arr) => arr.findIndex((i) => i.url === item.url) === index
);
const seen = new Set();
return allUrls.filter((item) => {
if (seen.has(item.url)) return false;
seen.add(item.url);
return true;
});
} catch (error) {
console.error('Error generating sitemap:', error);
return [];