86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { locationData, serviceData, blogPostData } from '../src/data/seoData';
|
|
|
|
const BASE_URL = process.env.BASE_URL || 'https://bayareait.services';
|
|
|
|
/**
|
|
* Generates the sitemap.xml content
|
|
*/
|
|
const generateSitemap = () => {
|
|
const currentDate = new Date().toISOString().split('T')[0];
|
|
|
|
let xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
`;
|
|
|
|
// Static Pages
|
|
const staticPages = [
|
|
'',
|
|
'/services',
|
|
'/blog',
|
|
'/contact',
|
|
'/about'
|
|
];
|
|
|
|
staticPages.forEach(page => {
|
|
xml += ` <url>
|
|
<loc>${BASE_URL}${page}</loc>
|
|
<lastmod>${currentDate}</lastmod>
|
|
<changefreq>monthly</changefreq>
|
|
<priority>${page === '' ? '1.0' : '0.8'}</priority>
|
|
</url>
|
|
`;
|
|
});
|
|
|
|
// Location Pages
|
|
locationData.forEach(page => {
|
|
xml += ` <url>
|
|
<loc>${BASE_URL}/${page.slug}</loc>
|
|
<lastmod>${currentDate}</lastmod>
|
|
<changefreq>weekly</changefreq>
|
|
<priority>0.9</priority>
|
|
</url>
|
|
`;
|
|
});
|
|
|
|
// Service Pages
|
|
serviceData.forEach(page => {
|
|
xml += ` <url>
|
|
<loc>${BASE_URL}/${page.slug}</loc>
|
|
<lastmod>${currentDate}</lastmod>
|
|
<changefreq>weekly</changefreq>
|
|
<priority>0.9</priority>
|
|
</url>
|
|
`;
|
|
});
|
|
|
|
// Blog Posts
|
|
blogPostData.forEach(post => {
|
|
xml += ` <url>
|
|
<loc>${BASE_URL}/blog/${post.slug}</loc>
|
|
<lastmod>${currentDate}</lastmod>
|
|
<changefreq>monthly</changefreq>
|
|
<priority>0.7</priority>
|
|
</url>
|
|
`;
|
|
});
|
|
|
|
xml += `</urlset>`;
|
|
return xml;
|
|
};
|
|
|
|
// Write to public/sitemap.xml
|
|
const sitemap = generateSitemap();
|
|
const outputPath = path.resolve(process.cwd(), 'public/sitemap.xml');
|
|
|
|
// Ensure public directory exists
|
|
const publicDir = path.dirname(outputPath);
|
|
if (!fs.existsSync(publicDir)) {
|
|
fs.mkdirSync(publicDir, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(outputPath, sitemap);
|
|
console.log(`✅ Sitemap generated at ${outputPath}`);
|