99 lines
3.2 KiB
JavaScript
99 lines
3.2 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { SITE_ORIGIN, DEFAULT_PATH } from '../src/config.js';
|
|
import { REGIONS_DATA } from '../src/data/regions.js';
|
|
import { TARGETS, SUBJECTS } from '../src/data/subjects.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const publicDir = path.resolve(__dirname, '../public');
|
|
|
|
function generateUrlXml(urls, today) {
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${urls
|
|
.map(
|
|
(u) => ` <url>
|
|
<loc>${u.loc}</loc>
|
|
<lastmod>${today}</lastmod>
|
|
<changefreq>${u.changefreq}</changefreq>
|
|
<priority>${u.priority}</priority>
|
|
</url>`
|
|
)
|
|
.join('\n')}
|
|
</urlset>
|
|
`;
|
|
}
|
|
|
|
function generate() {
|
|
const today = new Date().toISOString().split('T')[0];
|
|
const sitemapFiles = [];
|
|
let totalIndexedUrls = 0;
|
|
|
|
// 1. 공통 및 메인 대표 랜딩 페이지 사이트맵 (sitemap-main.xml)
|
|
const mainUrls = [
|
|
{ loc: `${SITE_ORIGIN}`, priority: '1.0', changefreq: 'daily' },
|
|
{ loc: `${SITE_ORIGIN}/regions`, priority: '0.9', changefreq: 'daily' },
|
|
{ loc: `${SITE_ORIGIN}${DEFAULT_PATH}`, priority: '1.0', changefreq: 'daily' }
|
|
];
|
|
const mainXml = generateUrlXml(mainUrls, today);
|
|
fs.writeFileSync(path.join(publicDir, 'sitemap-main.xml'), mainXml, 'utf-8');
|
|
sitemapFiles.push('sitemap-main.xml');
|
|
totalIndexedUrls += mainUrls.length;
|
|
|
|
// 2. 17개 시·도별 개별 사이트맵 (sitemap-${sidoSlug}.xml)
|
|
REGIONS_DATA.forEach((sido) => {
|
|
const sidoUrls = [];
|
|
sido.sigunguList.forEach((sigungu) => {
|
|
sigungu.dongList.forEach((dong) => {
|
|
TARGETS.forEach((target) => {
|
|
SUBJECTS.forEach((sub) => {
|
|
sidoUrls.push({
|
|
loc: `${SITE_ORIGIN}/${sido.sidoSlug}/${sigungu.sigunguSlug}/${dong.dongSlug}/${target.slug}/${sub.slug}`,
|
|
priority: '0.8',
|
|
changefreq: 'weekly'
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
const fileName = `sitemap-${sido.sidoSlug}.xml`;
|
|
const xml = generateUrlXml(sidoUrls, today);
|
|
fs.writeFileSync(path.join(publicDir, fileName), xml, 'utf-8');
|
|
sitemapFiles.push(fileName);
|
|
totalIndexedUrls += sidoUrls.length;
|
|
});
|
|
|
|
// 3. 루트 사이트맵 인덱스 (sitemap.xml - 구글/네이버 단일 제출용 표준)
|
|
const indexXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${sitemapFiles
|
|
.map(
|
|
(file) => ` <sitemap>
|
|
<loc>${SITE_ORIGIN}/${file}</loc>
|
|
<lastmod>${today}</lastmod>
|
|
</sitemap>`
|
|
)
|
|
.join('\n')}
|
|
</sitemapindex>
|
|
`;
|
|
fs.writeFileSync(path.join(publicDir, 'sitemap.xml'), indexXml, 'utf-8');
|
|
|
|
// 4. robots.txt
|
|
const robots = `User-agent: *
|
|
Allow: /
|
|
Disallow: /admin
|
|
Disallow: /regions/admin
|
|
|
|
Sitemap: ${SITE_ORIGIN}/sitemap.xml
|
|
`;
|
|
fs.writeFileSync(path.join(publicDir, 'robots.txt'), robots, 'utf-8');
|
|
|
|
console.log(`[OK] 사이트맵 인덱스(sitemap.xml) 및 ${sitemapFiles.length}개 시·도 분할 사이트맵 생성 완료!`);
|
|
console.log(`[OK] 총 인덱싱 등록 URL: ${totalIndexedUrls.toLocaleString()}개 (${SITE_ORIGIN})`);
|
|
}
|
|
|
|
generate();
|