Metatron-Website-India/scripts/generate-sitemap.cjs
2025-10-02 15:06:44 +05:30

113 lines
4.7 KiB
JavaScript

const fs = require('fs');
const { SitemapStream, streamToPromise } = require('sitemap');
const path = require('path');
const hostname = 'https://metatroncube.in/';
const addTrailingSlash = true; // ✅ Set this true if your Next.js uses trailingSlash: true
// // 🔧 Utility to format URLs based on config
// const formatUrl = (url) => {
// if (addTrailingSlash && !url.endsWith('/')) return url + '/';
// if (!addTrailingSlash && url.endsWith('/') && url !== '/') return url.slice(0, -1);
// return url;
// };
// Add a trailing slash only for “directory-like” URLs
const shouldAddSlash = (url) => {
// keep "/" as is
if (url === '/') return false;
// don't touch file-like URLs (has extension)
if (/\.[a-z0-9]{2,6}(\?.*)?$/i.test(url)) return false;
return true;
};
const formatUrl = (url) => {
// normalize to leading slash
if (!url.startsWith('/')) url = '/' + url;
if (addTrailingSlash && shouldAddSlash(url) && !url.endsWith('/')) {
return url + '/';
}
if (!addTrailingSlash && url.endsWith('/') && url !== '/') {
return url.slice(0, -1);
}
return url;
};
// ✅ Static pages
const staticLinks = [
{ url: '/', changefreq: 'daily', priority: 1.0 },
{ url: '/services-digital-solutions/', changefreq: 'daily', priority: 0.7 },
{ url: '/about/', changefreq: 'weekly', priority: 0.7 },
{ url: '/careers/', changefreq: 'weekly', priority: 0.7 },
{ url: '/portfolio/', changefreq: 'weekly', priority: 0.7 },
{ url: '/blog/', changefreq: 'weekly', priority: 0.7 },
{ url: '/contact/', changefreq: 'monthly', priority: 0.5 },
{ url: '/faq/', changefreq: 'monthly', priority: 0.5 },
{ url: '/service/website-development-company/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/mobile-application-development/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/graphic-designing-company/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/ui-ux-designing/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/search-engine-optimization-seo-content-writing/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/digital-marketing-agency-in-canada/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/app-development-waterloo/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/kitchener-waterloo-website-design-services/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/professional-website-designers-in-waterloo/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/waterloo-seo-services/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/web-design-toronto-custom-website-creation-by-metatroncube-software-solutions/', changefreq: 'weekly', priority: 0.6 },
{ url: '/service/web-page-design-in-waterloo/', changefreq: 'weekly', priority: 0.6 },
];
// ✅ Dynamic blog posts
const blogPosts = [
{ slug: '5-tips-to-create-viral-content-that-drives-engagement/' },
{ slug: 'instagram-vs-facebook-choosing-the-right-platform-for-your-business/' },
{ slug: 'how-local-seo-can-drive-more-foot-traffic-to-your-business/' },
{ slug: 'on-page-vs-off-page-seo-what-every-business-owner-needs-to-know/' },
{ slug: 'how-to-create-a-winning-digital-marketing-strategy-for-your-business/' },
{ slug: 'white-hat-vs-black-hat-seo-an-in-depth-link-building-guide/' },
{ slug: 'how-to-boost-your-small-business-with-effective-digital-marketing-strategies/' },
{ slug: 'the-importance-of-local-seo-for-real-estate-agents/' },
{ slug: 'how-to-optimize-your-website-for-voice-search/' },
{ slug: 'how-ai-is-revolutionizing-web-development-and-seo/' },
{ slug: 'top-digital-marketing-agency-in-canada-metatroncube-software-solutions/' },
{ slug: 'best-digital-marketing-company-in-canada-metatroncube-solutions/' },
{ slug: 'web-designers-for-small-business/' },
{ slug: 'mobile-commerce-2024-web-app-development-evolution/' },
];
// Convert blog slugs to sitemap entries
const blogLinks = blogPosts.map(post => ({
url: `/${post.slug}`,
changefreq: 'weekly',
priority: 0.6
}));
const allLinks = [...staticLinks, ...blogLinks].map(link => ({
...link,
url: formatUrl(link.url),
}));
async function generateSitemap() {
try {
const sitemap = new SitemapStream({ hostname: hostname });
const writeStream = fs.createWriteStream(path.resolve(__dirname, '../out/sitemap.xml'));
sitemap.pipe(writeStream);
console.log('📦 Writing URLs to sitemap:');
allLinks.forEach(link => {
console.log(' -', hostname + link.url);
sitemap.write(link);
});
sitemap.end();
await streamToPromise(sitemap);
console.log('✅ sitemap.xml created successfully!');
} catch (error) {
console.error('❌ Error creating sitemap.xml:', error);
}
}
generateSitemap();