62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
import { SitemapStream, streamToPromise } from 'sitemap';
|
|
import { Readable } from 'stream';
|
|
import { writeFileSync } from 'fs';
|
|
import axios from "axios";
|
|
|
|
async function generateSitemap() {
|
|
|
|
const API_URL = 'http://localhost:5000';
|
|
|
|
async function fetchArticlePaths() {
|
|
try {
|
|
const response = await axios.get(`${API_URL}/api/articles/sitemap/`);
|
|
return response.data.articles;
|
|
} catch (error) {
|
|
console.error('Ошибка при получении путей статей:', error.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
const baseUrl = 'https://russcult.anibilag.ru'; // Replace with your actual domain
|
|
|
|
// Create a stream to write to
|
|
const stream = new SitemapStream({ hostname: baseUrl });
|
|
|
|
// Add static routes
|
|
const staticRoutes = [
|
|
{ url: '/', changefreq: 'daily', priority: 1.0 },
|
|
{ url: '/search', changefreq: 'weekly', priority: 0.8 },
|
|
];
|
|
|
|
const articles = await fetchArticlePaths();
|
|
|
|
// Add dynamic article routes
|
|
const articleRoutes = articles.map(article => ({
|
|
url: `/article/${article.id}`,
|
|
changefreq: 'weekly',
|
|
priority: 0.7,
|
|
lastmod: article.publishedAt
|
|
}));
|
|
|
|
// Add category routes
|
|
const categories = ['Film', 'Theater', 'Music', 'Sports', 'Art', 'Legends', 'Anniversaries', 'Memory'];
|
|
const categoryRoutes = categories.map(category => ({
|
|
url: `/?category=${category}`,
|
|
changefreq: 'daily',
|
|
priority: 0.9
|
|
}));
|
|
|
|
// Combine all routes
|
|
const links = [...staticRoutes, ...articleRoutes, ...categoryRoutes];
|
|
|
|
// Create sitemap from routes
|
|
const sitemap = await streamToPromise(
|
|
Readable.from(links).pipe(stream)
|
|
).then(data => data.toString());
|
|
|
|
// Write sitemap to public directory
|
|
writeFileSync('./public/sitemap.xml', sitemap);
|
|
console.log('Sitemap generated successfully!');
|
|
}
|
|
|
|
generateSitemap().catch(console.error); |