63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const { SitemapStream, streamToPromise } = require("sitemap");
|
|
|
|
const hostname = "https://skyandsoil.metatronnest.com";
|
|
const addTrailingSlash = true;
|
|
|
|
const shouldAddSlash = (url) => {
|
|
if (url === "/") return false;
|
|
if (/\.[a-z0-9]{2,6}(\?.*)?$/i.test(url)) return false;
|
|
return true;
|
|
};
|
|
|
|
const formatUrl = (url) => {
|
|
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: "/" },
|
|
{ url: "/about/" },
|
|
{ url: "/projects/" },
|
|
{ url: "/lifestyle/" },
|
|
{ url: "/contact/" },
|
|
{ url: "/compare/" },
|
|
];
|
|
|
|
// ✅ Dynamic properties (IDs from src/data/properties.ts)
|
|
const propertyIds = [1, 2, 3, 4, 5];
|
|
const propertyLinks = propertyIds.map(id => ({ url: `/properties/${id}/` }));
|
|
|
|
// Combine static + dynamic links
|
|
const allLinks = [...staticLinks, ...propertyLinks].map(link => ({
|
|
url: formatUrl(link.url),
|
|
}));
|
|
|
|
async function generateSitemap() {
|
|
try {
|
|
const sitemap = new SitemapStream({ hostname });
|
|
const writeStream = fs.createWriteStream(path.resolve(__dirname, "../public/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();
|