@dijitrak/react-nextjs-seo-plugin
Version:
A modern, user-friendly SEO plugin for React and Next.js with multilingual support and comprehensive optimization tools
78 lines (67 loc) • 2.58 kB
text/typescript
/**
* Sitemap.xml oluşturma işlemleri
*/
/**
* Sitemap.xml oluşturur
* @param pages Sayfalar
* @param siteUrl Site URL'si
* @param excludedPaths Hariç tutulacak yollar
* @param customUrls Özel URL'ler
* @returns Sitemap.xml içeriği
*/
export function generateSitemapXml(
pages: Array<{ path: string; lastModified?: string; priority?: number; changeFreq?: string }>,
siteUrl: string,
excludedPaths: string[] = [],
customUrls: Array<{ loc: string; lastmod?: string; priority?: number; changefreq?: string }> = []
): string {
// URL sonunda / varsa kaldır
siteUrl = siteUrl.replace(/\/$/, '');
let content = '<?xml version="1.0" encoding="UTF-8"?>\n';
content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
// Sayfaları ekle
if (pages && pages.length > 0) {
pages.forEach(page => {
// Hariç tutulan bir yol mu kontrol et
if (excludedPaths.some(excludedPath => page.path.startsWith(excludedPath))) {
return;
}
// /api veya /_next ile başlayan yolları atla
if (page.path.startsWith('/api/') || page.path.startsWith('/_next/')) {
return;
}
const pagePath = page.path === '/' ? '' : page.path;
const loc = siteUrl + pagePath;
const lastmod = page.lastModified || new Date().toISOString().split('T')[0];
const priority = page.priority || 0.5;
const changefreq = page.changeFreq || 'weekly';
content += ' <url>\n';
content += ` <loc>${loc}</loc>\n`;
content += ` <lastmod>${lastmod}</lastmod>\n`;
content += ` <priority>${priority}</priority>\n`;
content += ` <changefreq>${changefreq}</changefreq>\n`;
content += ' </url>\n';
});
}
// Özel URL'leri ekle
if (customUrls && customUrls.length > 0) {
customUrls.forEach(url => {
// URL'yi düzenle
let fullUrl = url.loc;
if (!fullUrl.startsWith('http')) {
fullUrl = siteUrl + (fullUrl.startsWith('/') ? fullUrl : `/${fullUrl}`);
}
const lastmod = url.lastmod || new Date().toISOString().split('T')[0];
const priority = url.priority || 0.5;
const changefreq = url.changefreq || 'weekly';
content += ' <url>\n';
content += ` <loc>${fullUrl}</loc>\n`;
content += ` <lastmod>${lastmod}</lastmod>\n`;
content += ` <priority>${priority}</priority>\n`;
content += ` <changefreq>${changefreq}</changefreq>\n`;
content += ' </url>\n';
});
}
content += '</urlset>';
return content;
}