добавил скрипт для пререндеринга и карту сайта

This commit is contained in:
Joe_Peach committed 2026-04-12 00:36:08 +03:00
1 parent 3bf10b0e52
commit b7027d1183
5 files changed
+815 -2503

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Список всех страниц
const routes = [
'/',
'/products',
'/about',
// добавьте свои URL
];
const distPath = resolve(__dirname, '../dist');
const indexHtmlPath = resolve(distPath, 'index.html');
if (!existsSync(indexHtmlPath)) {
console.error('❌ Сначала запустите npm run build');
process.exit(1);
}
// Читаем исходный HTML
let baseHtml = readFileSync(indexHtmlPath, 'utf-8');
// Добавляем базовые мета-теги для каждой страницы
const pages = {
'/': {
title: 'Магазин труб - Главная',
description: 'Купить канализационные трубы по низким ценам'
},
'/products': {
title: 'Каталог труб для канализации',
description: 'Широкий выбор труб, фитингов и комплектующих'
},
'/about': {
title: 'О магазине труб',
description: 'Информация о нашей компании'
}
};
routes.forEach(route => {
const pageInfo = pages[route] || pages['/'];
// Заменяем мета-теги в HTML
let html = baseHtml
.replace(/<title>.*?<\/title>/, `<title>${pageInfo.title}</title>`)
.replace(
/<meta name="description".*?>/,
`<meta name="description" content="${pageInfo.description}">`
);
// Определяем путь для сохранения
let outputPath;
if (route === '/') {
outputPath = indexHtmlPath;
} else {
const routeDir = resolve(distPath, route.substring(1));
if (!existsSync(routeDir)) {
mkdirSync(routeDir, { recursive: true });
}
outputPath = resolve(routeDir, 'index.html');
}
writeFileSync(outputPath, html);
console.log(`✅ ${route} → ${outputPath}`);
});
console.log('✨ Done!');