добавил скрипт для пререндеринга и карту сайта
This commit is contained in:
1 parent
3bf10b0e52
commit
b7027d1183
5 files changed
+815
-2503
No files matched your search
Generated
+731
-2500
File diff suppressed because it is too large.
Load diff
+3
-1
@@ -8,6 +8,8 @@
|
||||
"build": "vite build",
|
||||
"build:check": "tsc -b && vite build",
|
||||
"lint": "eslint .",
|
||||
"prerender": "node scripts/prerender.js",
|
||||
"build:seo": "npm run build && npm run preview & sleep 5 && npm run prerender && kill $(lsof -t -i:4173)",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -39,10 +41,10 @@
|
||||
"eslint-plugin-react-hooks": "^5.2.0",
|
||||
"eslint-plugin-react-refresh": "^0.4.22",
|
||||
"globals": "^16.4.0",
|
||||
"puppeteer": "^24.40.0",
|
||||
"typescript": "~5.9.3",
|
||||
"typescript-eslint": "^8.45.0",
|
||||
"vite": "^7.1.7",
|
||||
"vite-plugin-prerender": "^1.0.8",
|
||||
"vite-plugin-sitemap": "^0.8.2"
|
||||
}
|
||||
}
|
||||
@@ -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!');
|
||||
+1
-1
@@ -24,5 +24,5 @@
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedSideEffectImports": true
|
||||
},
|
||||
"include": ["src"]
|
||||
"include": ["src", "scripts"]
|
||||
}
|
||||
+11
-1
@@ -1,9 +1,19 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
import Sitemap from 'vite-plugin-sitemap';
|
||||
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
plugins: [
|
||||
react(),
|
||||
Sitemap({
|
||||
hostname: 'https://pvh.projects.rustprogersteam.ru',
|
||||
exclude: ['/admin', '/profile'],
|
||||
readable: true,
|
||||
outDir: './dist',
|
||||
}),
|
||||
],
|
||||
server: {
|
||||
host: '0.0.0.0', // Слушаем все интерфейсы
|
||||
port: 5173,
|
||||
|
||||
Reference in new issue
Block a user