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
|
||||
function getCurrentDomain() {
|
||||
const headersList = headers();
|
||||
const host = headersList.get('host');
|
||||
return host || DOMAIN_CONFIG.MAIN_DOMAIN;
|
||||
try {
|
||||
const headersList = headers();
|
||||
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
|
||||
@@ -225,12 +231,17 @@ export default async function sitemap() {
|
||||
const domain = getCurrentDomain();
|
||||
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
|
||||
const staticPages = getStaticPages(baseUrl);
|
||||
const doctorSearchUrls = getDoctorSearchUrls(domain, baseUrl);
|
||||
const clinicSearchUrls = getClinicSearchUrls(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
|
||||
const allUrls = [
|
||||
...staticPages,
|
||||
@@ -244,6 +255,7 @@ export default async function sitemap() {
|
||||
arr.findIndex(i => i.url === item.url) === index
|
||||
);
|
||||
|
||||
console.log(`Total unique URLs: ${uniqueUrls.length}`);
|
||||
return uniqueUrls;
|
||||
} catch (error) {
|
||||
console.error('Error generating sitemap:', error);
|
||||
|
||||
+14
-2
@@ -6,7 +6,7 @@ export const DOMAIN_CONFIG = {
|
||||
};
|
||||
|
||||
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) {
|
||||
@@ -24,7 +24,19 @@ export function createSitemapUrl(baseUrl, path, params = {}) {
|
||||
});
|
||||
|
||||
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') {
|
||||
|
||||
Reference in New Issue
Block a user