Files
PVHFrontend/scripts/prerender.js
T

69 lines
2.0 KiB
JavaScript

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!');