fix: enhance domain handling in sitemap generation and add XML escaping utility
This commit is contained in:
+15
-3
@@ -13,9 +13,15 @@ import {
|
|||||||
|
|
||||||
// Get current domain from headers
|
// Get current domain from headers
|
||||||
function getCurrentDomain() {
|
function getCurrentDomain() {
|
||||||
const headersList = headers();
|
try {
|
||||||
const host = headersList.get('host');
|
const headersList = headers();
|
||||||
return host || DOMAIN_CONFIG.MAIN_DOMAIN;
|
const host = headersList.get('host');
|
||||||
|
return host || DOMAIN_CONFIG.MAIN_DOMAIN;
|
||||||
|
} catch (error) {
|
||||||
|
// Fallback for static generation
|
||||||
|
console.warn('Headers not available during static generation, using main domain');
|
||||||
|
return DOMAIN_CONFIG.MAIN_DOMAIN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get city data based on domain
|
// Get city data based on domain
|
||||||
@@ -225,12 +231,17 @@ export default async function sitemap() {
|
|||||||
const domain = getCurrentDomain();
|
const domain = getCurrentDomain();
|
||||||
const baseUrl = getBaseUrl(domain);
|
const baseUrl = getBaseUrl(domain);
|
||||||
|
|
||||||
|
console.log(`Generating sitemap for domain: ${domain}, baseUrl: ${baseUrl}`);
|
||||||
|
console.log(`City data length: ${cityData.length}, Specialty data length: ${specialtyData.length}`);
|
||||||
|
|
||||||
// Get all URLs
|
// Get all URLs
|
||||||
const staticPages = getStaticPages(baseUrl);
|
const staticPages = getStaticPages(baseUrl);
|
||||||
const doctorSearchUrls = getDoctorSearchUrls(domain, baseUrl);
|
const doctorSearchUrls = getDoctorSearchUrls(domain, baseUrl);
|
||||||
const clinicSearchUrls = getClinicSearchUrls(domain, baseUrl);
|
const clinicSearchUrls = getClinicSearchUrls(domain, baseUrl);
|
||||||
const specialtyUrls = getSpecialtyUrls(domain, baseUrl);
|
const specialtyUrls = getSpecialtyUrls(domain, baseUrl);
|
||||||
|
|
||||||
|
console.log(`Generated URLs - Static: ${staticPages.length}, Doctors: ${doctorSearchUrls.length}, Clinics: ${clinicSearchUrls.length}, Specialties: ${specialtyUrls.length}`);
|
||||||
|
|
||||||
// Combine all URLs
|
// Combine all URLs
|
||||||
const allUrls = [
|
const allUrls = [
|
||||||
...staticPages,
|
...staticPages,
|
||||||
@@ -244,6 +255,7 @@ export default async function sitemap() {
|
|||||||
arr.findIndex(i => i.url === item.url) === index
|
arr.findIndex(i => i.url === item.url) === index
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(`Total unique URLs: ${uniqueUrls.length}`);
|
||||||
return uniqueUrls;
|
return uniqueUrls;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error generating sitemap:', error);
|
console.error('Error generating sitemap:', error);
|
||||||
|
|||||||
+14
-2
@@ -6,7 +6,7 @@ export const DOMAIN_CONFIG = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function isMainDomain(domain) {
|
export function isMainDomain(domain) {
|
||||||
return domain === DOMAIN_CONFIG.MAIN_DOMAIN;
|
return domain === DOMAIN_CONFIG.MAIN_DOMAIN || domain.includes('localhost') || domain.includes('127.0.0.1');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeUrlParam(param) {
|
export function encodeUrlParam(param) {
|
||||||
@@ -24,7 +24,19 @@ export function createSitemapUrl(baseUrl, path, params = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const queryString = searchParams.toString();
|
const queryString = searchParams.toString();
|
||||||
return queryString ? `${url}?${queryString}` : url;
|
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
||||||
|
|
||||||
|
// Escape XML entities
|
||||||
|
return escapeXml(fullUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function escapeXml(str) {
|
||||||
|
return str
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createSitemapEntry(url, priority = 0.8, changeFrequency = 'weekly') {
|
export function createSitemapEntry(url, priority = 0.8, changeFrequency = 'weekly') {
|
||||||
|
|||||||
Reference in New Issue
Block a user